0% found this document useful (0 votes)
80 views6 pages

PHP String Functions:: Example

The document discusses various PHP concepts including string functions, conditional statements, looping statements, arrays, functions, and more. String functions covered include strlen(), str_word_count(), strrev(), and str_replace(). Conditional statements covered include if, if else, if else if, and switch. Looping statements include for, while, do while, and foreach. The document also provides examples of indexed arrays, associative arrays, multidimensional arrays, and array sorting functions. User-defined functions are demonstrated including function arguments and default arguments.

Uploaded by

Reshma Chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views6 pages

PHP String Functions:: Example

The document discusses various PHP concepts including string functions, conditional statements, looping statements, arrays, functions, and more. String functions covered include strlen(), str_word_count(), strrev(), and str_replace(). Conditional statements covered include if, if else, if else if, and switch. Looping statements include for, while, do while, and foreach. The document also provides examples of indexed arrays, associative arrays, multidimensional arrays, and array sorting functions. User-defined functions are demonstrated including function arguments and default arguments.

Uploaded by

Reshma Chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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