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

75-PHP-Strings and String Function-Arrays-08-10-2024

The document contains multiple PHP scripts demonstrating user-defined functions, string manipulation, and array handling. It showcases various functionalities such as calculating costs, string operations like explode and implode, and working with multi-dimensional arrays. Additionally, it illustrates the use of loops and conditionals in PHP.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

75-PHP-Strings and String Function-Arrays-08-10-2024

The document contains multiple PHP scripts demonstrating user-defined functions, string manipulation, and array handling. It showcases various functionalities such as calculating costs, string operations like explode and implode, and working with multi-dimensional arrays. Additionally, it illustrates the use of loops and conditionals in PHP.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

<!

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");

function sum($x, $y) {


$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";

function calculateAmt($cost, $num)


{
return ($cost * $num);
}

$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>";
}

Echo "(bfr fn call) Cost is $cost <br>";


calculateCost($cost, $tax);
Echo "Tax is ".$tax*100,"<br>";
Echo "(aft fn call) Cost is: $cost";
?>
</body>
</html>
<!DOCTYPE html>
<html>
<body>

<?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>";

$str = " Hello World!";


echo $str ."<br>";
echo "ltrim->".ltrim($str,"Hello");

$str = "Hello World! ";


//echo $str . "<br>";
echo "rtrim->".rtrim($str,"World!");

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 "stristr-->".strstr("Hello world! Have a nice day","world");

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);

for($x = 0; $x < $arrlength; $x++) {


echo $cars[$x];
echo "<br>";
}

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");


foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}

$cars = array(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";


echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";

$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>

You might also like