<?php
# String Functions
// Function strlen() returns the length of the string, example:
echo strlen ("Nice to meet you!") . "\n"; /* output - 17 */
// Function strrev() returns the Reverse of the string, example:
echo strrev ("Nice to meet you!") . "\n"; /* output - !uoy teem ot eciN */
// Function strpos() tries to find the second parameter and returns its index. example:
echo strpos ("Nice to meet you", "you") . "\n"; /* output - 13 */;
// Function str_replace() replaces the word "Hello" with "Hi" , example:
echo ("Hello", "Hi", "Hello there") . "\n"; // outputs - Hi there;
// Function substr() returns the substring between given indexes, example:
echo substr("Nice to meet you", "5") . "\n"; // outputs - to meet you;
echo substr("Nice to meet you", 5, 3) . "\n"; // outputs - to meet you;
// Function trim() removes whitespaces from sides of the string, example:
echo trim(" Nice to meet you ") . "\n"; // outputs - Nice to meet you;
// Function strtoupper() converts the string to upper case, example:
echo strtoupper("Nice to meet you") . "\n"; //outputs - NICE TO MEET YOU;
// Function strtolower() converts the string to lower case, example:
echo strtolower("NICE To Meet You") ; //outputs - nice to meet you;
// Function explode() breaks the string by its delimiter, example:
print_r (explode(" ", "It's the first day of the course!"));
// Function implode() joins the array element based on the delimiter and returns the string, example:
echo implode(" ", ['Nice', 'to', 'meet', 'you']); //outputs - Nice to meet you;
// The PHP str_word_count() function counts the number of words in a string, example:
echo str_word_count("Nice to meet you!"); // outputs - 4
?>
$x = 100;
$y = 11;
• Addition (Sum of $x and $y)
echo $x + $y . "\n";
• Subtraction (Difference of $x and $y)
echo $x - $y . "\n";
• Multiplication (Product of $x and $y)
echo $x * $y . "\n";
• Division (Quotient of $x and $y)
echo $x / $y . "\n";
• Modulus (Remainder of $x divided by $y)
echo $x % $y . "\n";
• Exponentiation (Result of raising $x to the $y'th power)
echo $x ** $y . "\n";
name = "No name";
$1x = 20;
$)name = "No name";
$na me = "Hola";
&na-me = "Hello";
Correct syntax:$name = "No name";
$x1 = 20;
$_name = "Hello";
$
sign.
For instance:
$firstname = "Levani";
$lastname = "Makhareishvili";
$age = 19;
• Assignment Operator (=) is used to assign the value to a variable.
• the variable $firstname will hold the value "Levani",
• the variable $lastname will hold the value "Makhareishvili" and so on.
To output the variable on the screen, we write the name of the variable:
echo $age;
When we want to output the value of the variable together with the text, we write the name of the variable
in double quotes or after the text we write a comma or a dot and then the name of the variable:
echo "My name is $firstname $lastname, I am $age years old";
echo "My name is ",$firstname," ",$lastname,", I am ".$age. " years old.";
! PHP statements end with semicolons (;).$x++; // equivalent to $x = $x+1;
$x--; // equivalent to $x = $x-1;
$x++; // post-increment
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement
The difference is that the post-increment returns the original value before it changes the variable, while the pre-increment changes the variable first and then returns the value.
Example:
$a = 2; $b = $a++; // $a=3, $b=2
$a = 2; $b = ++$a; // $a=3, $b=3