creating and accessing string in PHP |
Creating and accessing string in PHP
String function
1 strlen(): It is used to find the length of string.
<?php $str="Icoderweb"; echo strlen($str); ?> *****OUTPUT***** 9 |
2 str_word_count(): It
is used to count the number of words in given string.
<?php $str="i am a
Software Developer"; echo
str_word_count($str); ?> 5 |
3 strpos(): It
is used to search a text within a string. If the match is found, the function returns the
character position of the first match.
$str="i am a
software developer"; echo
strpos($str,"software"); ?> *****OUTPUT***** 7 |
4 str_replace(): It
is used to replace the existing text with given text in a string.
<?php $str="Icoderweb
website is a Learning programming "; //Replace the text
"telecom" with "software": echo str_replace("website","software",$str);
?> *****OUTPUT***** Icoderweb software is a
Learning programming |
5 strrev(): It is used to reverse the
characters of string.
<?php $str="icoderweb";
echo strrev($str); ?> *****OUTPUT***** bewredoci |
6 strtoupper(): It is used to convert all
the alphabet of string into uppercase alphabet.
<?php $str="icoderweb";
echo strtoupper($str); ?> *****OUTPUT***** ICODERWEB |
7 strtolower(): It is used to convert all the alphabet of string into lowercase alphabet.
<?php $str="ICODERWEB";
echo strtolower($str); ?> *****OUTPUT***** Icoderweb |
8 ucfirst(): It is used to convertsthe first character of a string to uppercase.
<?php $str="icoderweb";
echo ucfirst($str); ?> *****OUTPUT***** Icoderweb |
9 lcfirst(): Converts the first character of a string to lowercase.
<?php $str="Icoderweb";
echo lcfirst($str); ?> *****OUTPUT***** Icoderweb |
10 substr(): It is used to get some parts of the string.
<?php $str="ICODERWEB
WEBSITE"; echo substr($str,5,4); ?> *****OUTPUT***** RWEB |
11 str_repeat(): It
is used to repeats string a specified number of times.
<?php echo
str_repeat("Icoderweb ",5); ?> *****OUTPUT***** Icoderweb Icoderweb Icoderweb Icoderweb Icoderweb |
12 implode(): It is used to join the array elements with string.
<?php $students=array("icoderweb","website","programming","learning");
echo
implode(",",$students); ?> *****OUTPUT***** Icoderweb,website,programming,learning
|
13 explode(): It is used to break the string into array.
<?php $str = "Have a nice
day"; print_r(explode("
",$str)); ?> *****OUTPUT***** Array ( [0] => Have [1] => a [2] => nice [3] => day ) |
14 strcmp(): It is used to compare two string. If both strings are same returns zero otherwise non-zero.
<?php $str1 = "icoderweb";
$str2="icoderweb";
if(strcmp($str1,$str2)==0)
echo "Both are
equal"; else echo "Both are not
equal"; ?> *****OUTPUT***** Both are equal |
Math function: A math function can be used mathematical operation performs. It is very most useful because all operations and calculations use math functions.
Read More Language String Function
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjPyt6qtFTmS8cRqs3MdCCUeb0hbfi3lB4CHvzlrpYU6TPCPmOnjiJucYuHGlul1HleuyGqdX2V4fQ3dbrYxQCRgQ_CFrmIwmmhgmZ6Q9SE3qA167nDy_IRz-_sPLtF1DRFT3T3XIlTDOzy/w30-h14/newicon.gif)
C String Functions
<?php echo
sin(2)."<br>"; echo
cos(2)."<br>"; echo
tan(2)."<br>"; echo
exp(2)."<br>";//exponential function echo
sqrt(4)."<br>";//square root function echo
log(2)."<br>";//natural log function echo
log10(2)."<br>"; echo
pow(2,3)."<br>";//power function echo
pi()."<br>";//return PI value /* ?> *****OUTPUT***** 0.909 -0.416 -2.185 7.389 2 0.693 0.301 8 3.141 |
rand(): It is used to generate random integer values between a given range. Nowadays it is mostly used to generate one-time passwords (OTP).
<?PHP echo rand(1111,9999); ?> *****OUTPUT***** 3379 you will get new value
every time |
max(): It returns the highest value in an array.
<?php $ar=array(58,51,95,36,15,45,67);
echo "Max Value is:".max($ar); ?> *****OUTPUT***** Max Value is:95 |
min(): It returns the minimum value in an array.
<?php $ar=array(58,51,95,36,15,45,67);
echo "Min Value is
".min($ar); ?> *****OUTPUT***** Min Value is 15 |
floor(): It is used to round a number down to the nearest integer.
<?php echo
floor(2.2)."<br>"; echo
floor(2.5)."<br>"; echo
floor(2.9)."<br>"; ?> *****OUTPUT***** 2 2 2 |
ceil(): It is used to round a number up to the nearest integer.
<?php echo
ceil(2.2)."<br>"; echo
ceil(2.5)."<br>"; echo
ceil(2.9)."<br>"; ?> *****OUTPUT***** 3 3 3 |
round(): It is used to round a floating point number.
<?php echo
round(2.2)."<br>"; echo
round(2.5)."<br>"; echo
round(2.9)."<br>"; ?> *****OUTPUT***** 2 3 3 |
abs(): It always returns positive value.
<?php echo
abs(2)."<br>"; echo
abs(-2)."<br>"; ?> *****OUTPUT***** 2 2 |