PHP String Functions:: Example

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

PHP String Functions:

Example:
<!DOCTYPE html>
<html>
<body>
<?php
echo strlen("Linda Lawrence");// returns string length
echo str_word_count("Linda Lawrence"); // returns number of words
echo strrev("Linda Lawrence");// reverse the given string
echo str_replace("Lawrence", "Lisa", "Linda Lawrence"); // replace first arg with second arg in given string
echo strstr("hai iam ram","ram"); //returns the occurrence of
?>
</body>
</html>

Output:
14
2
ecnerwaL adniL
Linda Lisa
ram

PHP Conditional Statements:


1.if
2. if else
3.switch
If:
Example:
<?php
$a=$_GET['txt1'];
if($a>5)
{
echo "hai";
}
echo " This is good";

?>
If else:
Example:
<?php
$a=10;
if($a>5)
{
echo "hai";
}
else
{
echo " This is good";
}

?>
If else if:
Example:
<?php
$t=date("H");
if ($t<"10")
{
echo "Have a good morning!";
}
elseif ($t<"20")
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
?>
Switch:
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$favcolor="yellow";
switch ($favcolor)
{
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, or green!";
}
?>
</body>
</html>

Looping Statements:
1.for
2.while
3.do while
4. for each
For:
Example:
<!DOCTYPE html>
<html>
<body>
<?php
for ($x=6; $x<=10; $x++)
{
echo "The number is: $x";
}
?>
</body>
</html>
While:
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$x=10;
while($x<=15)
{
echo "The number is: $x ";
$x++;
}
?>
</body>
</html>

Do While:
Example:

<html>
<body>
<?php
$x=1;
do
{
echo "The number is: $x";
$x++;
}while($x<=5);
?>
</body>
</html>

Foreach:
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $value)
{
echo "$value";
}
?>
</body>
</html>
Output:
Red green blue yellow

Arrays in PHP:
Indexed array:
Example:
<html>
<body>
<?php
$cars=array("volvo","audi","benz");
echo $cars[0];
echo $cars[1]";
echo $cars[2]";
echo "length:".count($cars)."<br>";

?>
</body>
</html>
Associative array:
Associative array is set of key value pairs
Example:

<?php
$age=array("vinodh"=>"15","vishnu"=>"21","sekhar"=>"56");

echo "vinodh is " . $age['vinodh'] . " years old.<br>";


echo "vishnu is " . $age['vishnu'] . " years old.";
?>
Multidimensional array:
Example:

<html>
<body>

<?php
$cars=array(array("ram","sam","beema"),array("harry","poter","bills"),array("dimitri","yong","tong"));

for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $cars[$i][$j];
}
}
?>
</body>
</html>

Array sorting functions:


Example:
<?php
$a=array(4,2,3,1);
$b=array("key1"=>5,"key2"=>1,"key3"=>3);
sort($a);
print_r($a);
rsort($a);
print_r($a);
asort($b);
print_r($b);
$c=array("key2"=>5,"key1"=>1,"key3"=>3);
ksort($c);
print_r($c);
?>

Functions:

user defined functions

syntax:

function functionName() {
    code to be executed;
}

example:

<html>
<body>

<?php
function writeMsg()
{
echo "Hello world!</br>";
}

writeMsg();
writemsg();
?>

</body>
</html>

function arguments:
syntax:

function functionName(arg1,arg2,....,argn) {
    code to be executed;
}

example:
<html>
<body>

<?php
function familyName($fname)
{
echo "$fname is Good!!</br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>
</body>
</html>

Default argument:
Example:

<html>
<body>

<?php
function setHeight($sec,$minheight=50)
{
echo "The height is : $minheight <br>";
echo "The sec is : $sec <br>";
}

setHeight(350);
#setHeight();//will use default value of defualt .
setHeight(135,175);
#setHeight(40);
?>

</body>
</html>

You might also like