You are on page 1of 2

// length of string

<?
$string = "hello world";

$length = strlen($string);

echo" the length of string is: " .$length."\n";


?>

//count no of words
<?php
$string = "hello, world! this is a sample string";

//initialize word count to 1


$wordcount=1;

//loop through each character in string


for ($i =0; $i < strlen($string); $i++)
{
if ($string[$i] == ' ')
{
$wordcount++;
}
}
echo "The number of words in the string is:"
$wordCount. "\n";
?>

// maths functions
<?php
$num1= 10;
$num2= 5;

//addition
$sum= $num1 +$sum2;
echo "sum:" .$sum ."\n";

//Substraction
$sub = $num1 - $num2;
echo "substraction:" .$sub ."\n";

//Multiplication
$mul = $num1 * $num2;
echo "Product:" $mul ."\n";

//Division
$division = $num1 / $num2;
echo"Quotient:" $division ."\n";

//Power
$power = pow($num1,$num2);
echo "Power:" $power .'\n";

//Square root
$sqrt = sqrt($num1);
echo "square root;" .$sqrt ."\n";
?>

//parameterize

<?php

function calculateSum ($num1, $num20


{
$sum = $num1 + $num2;
return $sum;
}

//call the function with arguments and store the result in variable
$result = calculateSum (10,5);

echo "The sum of 10 and 5 is :" .$result ."\n";


?>

//

You might also like