75-PHP-Strings and String Function-Arrays-08-10-2024
75-PHP-Strings and String Function-Arrays-08-10-2024
DOCTYPE html>
<html>
<body>
<h2>user defined funcion</h2>
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight();
function familyName($fname) {
echo "$fname.<br>";
}
familyName("Jani");
familyName("Hege");
function familyName1($fname1,$year) {
echo "$fname1 . Born in $year <br>";
}
familyName1("Hege", "1975");
familyName1("Stale", "1978");
$price=10;
$tot=5;
Echo "Total Amount". calculateAmt($price, $tot);
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>user defined function - call by reference</h2>
<?php
$cost = 20.99;
$tax = 0.0575;
function calculateCost(&$cost, $tax)
{
echo $cost."inside<br>";
// Modify the $cost variable
$cost = $cost + ($cost * $tax);
// Perform some random change to the $tax variable.
$tax += 4;
echo $cost."<br>";
}
<?php
/*
$str = "Hello World!";
print_r(count_chars($str,0));
echo "<br>";
print_r(count_chars($str,1));
echo "<br>";
print_r(count_chars($str,2));
echo "<br>";
echo count_chars($str,3);
echo "<br>";
echo count_chars($str,4);
*/
echo "<br>";
$str = "Hello world. It's a beautiful day.";
echo "explode-->";
print_r(explode(" ",$str));
echo "<br>";
$arr = array('Hello','World!','Beautiful','Day!');
echo "implode-->".implode(" ",$arr);
echo "<br>";
$arr = array('Hello','World!','Beautiful','Day!');
echo "join->".join(" ",$arr)."<br>";
echo "<br>";
echo str_shuffle("Hello World");
echo "<br>";
echo "strlen->".strlen("Hello world!");
echo "<br>";
echo "strrev->".strrev("Hello world!");
echo "<br>";
$a= "hello World!";
echo "strtoU pper-->".strtoupper($a)."<br>";
echo "strtolower-->".strtolower($a);
echo "<br>";
echo "strpos->".strpos("Hello world!", "l")."<br>";
echo "strrpos-->".strrpos("Hello world!","l")."<br>";
echo "strripos-->".strripos("HelLo world!","L")."<br>";
echo "<br>";
echo "substr-->".substr("Hello world!",3,2)."<br>";
$a= "hello World!";
echo substr($a,3,2)."<br>";
echo substr($a,-3)."<br>";
echo substr($a,-3,1)."<br>";
echo substr($a,0,-3)."<br>";
echo "<br>";
echo "substr_count-->".substr_count("Hello world!","lo");
echo "<br>";
echo "substr_replace-->".substr_replace("Hello world!","aaaaaa",2,6);
echo "<br>";
$a= "hello world!";
echo "ucfirst-->".ucfirst($a),"<br>";
echo "ucwords-->".ucwords($a);
echo "lcwords-->".lcfirst($a);
echo "<br>";
//parse_str("id=23&name=Kai Jim");
//echo $id."<br>";
//echo $name;
echo "<br>";
echo "str_replace->".str_replace("world", "Dolly", "Hello world!",$i);
echo "<br>";
echo "Replacements: $i";
echo "<br>";
echo "str_ireplace->".str_ireplace("WORLD","Peter","Hello world!",$j);
echo "<br>";
echo "Replacements: $j";
echo "<br>";
$str = "Hello World";
echo "str_pad-->".str_pad($str,20,".",STR_PAD_LEFT);
echo "<br>";
echo "str_pad-->".str_pad($str,20,"@",STR_PAD_BOTH);
echo "<br>";
echo "str_pad-->".str_pad($str,20,"$",STR_PAD_RIGHT);
echo "<br>";
echo "str_split-->";
print_r(str_split("Hello world!",1));
echo "<br>";
echo "str_word_count->".str_word_count("Hello world! slslslk");
echo "<br>";
echo "strcasecmp-->".strcasecmp("Hello world!","HELLO WORLD!")."<br>"; // The two strings are
equal
echo strcmp("hello","HELLO")."<br>"; // String1 is greater than string2
echo strcasecmp("Hello world!","HELLO WORLD! HELLO!")."<br>"; // String1 is less than string2
echo "<br>";
$str = "Hello World!";
echo $str . "<br>";
echo "chop->".chop($str,"World!");
echo "<br>";
echo "stripslashes-->".stripslashes("Who\'s Peter Griffin?");
echo "<br>";
echo "similar_text-->".similar_text("Hello World","Hello Peter");
echo "<br>";
similar_text("Hello World","Hello Peter",$percent);
echo $percent;
echo "<br>";
echo str_repeat(".",13);
?>
</body>
</html>
<!DOCTYPE html>
<html>
<!--PHP arrays-->
<body>
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
echo count($cars);
$arrlength=count($cars);
$cars = array(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
$marks = array("mohammad" => array ( "physics" => 35, "maths" => 30, "chemistry" => 39 ),
"john" => array(10,20,30 ),
"kumar" => array("physics" => 31, "maths" => 22, "chemistry" => 39 ) );
//Accessing multi-dimensional array values
echo "Marks for mohammad in physics : " ;
echo $marks['mohammad']['physics'] . "<br />";
echo "Marks for john in maths : ";
echo $marks['john'][1] . "<br />"; //numerical array
echo "Marks for kumar in chemistry : " ;
echo $marks['kumar']['chemistry'] ; //associative array
$colors = array("red","green","blue","yellow");
foreach($colors as $value)
{
echo "$value <br>";
}
?>
</body>
</html>