<?php
$name = "Tornike"; // global scope
function greeting() {
echo "Hello " . $name . "!"; // using $name will generate an error
}
greeting();
echo "Hello" . $name . "!"; // using $name will generate output (Hello Tornike!)
?>
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
<?php
function greeting() {
$name = "Tornike"; // local scope
echo "Hello " . $name . "!"; // using $name inside the function will generate output (Hello Tornike!)
}
greeting();
echo "Hello" . $name . "!"; // using $name outside the function will generate an error
?>
date_default_timezone_set('Asia/Tbilisi');
• d - Represents the day of the month (01 to 31)
echo date("d"); // outputs: current day of the month (01 to 31)
• D - A textual representation of a day (three letters)
echo date("D"); // outputs: currnet day (three letters)
• m - Represents a month (01 to 12)
echo date("m"); // outputs: current month (01 to 12)
• Y - Represents a year (in four digits)
echo date("Y"); // outputs: current year (in four digits)
• l - Represents the day of the week
echo date("l"); // outputs: current day (A full textual)
* echo "Today is " . date("Y/m/d"); // outputs: Today is 2022/10/17
echo "Today is " . date("Y.m.d"); // outputs: Today is 2022.10.17
echo "Today is " . date("Y-m-d"); // outputs: Today is 2022-10-17
echo "Today is " . date("l"); // outputs: Today is Monday
• H - 24-hour format of an hour (00 to 23)
echo date("H"); // outputs: current hour (00 to 23)
• h - 12-hour format of an hour with leading zeros (01 to 12)
echo date("h"); // outputs: currnet hour (01 to 12)
• i - Minutes with leading zeros (00 to 59)
echo date("i"); // outputs: current minutes (00 to 59)
• s - Seconds with leading zeros (00 to 59)
echo date("s"); // outputs: current seconds (00 four 59)
• a - Lowercase Ante meridiem and Post meridiem (am or pm)
echo date("a"); // outputs: current am/pm
// Correct Syntax ($variable_name = "Value";)
$my_car = "BMW";
// Incorect Syntax
my_car = "BMW";
$7my_car = "BMW";
$my&car = "BMW";
pi()
// pi() function returns the value of PI:
echo pi(); // outputs 3.1415926535898
• min()
• max()
// min() and max() functions can be used to find the lowest or highest value in a list of arguments:
echo (min(1, 4, 35, -9, 48)); // outputs -9
echo (max(41, 12, 89, 23, -19)); // outputs 89
• abs()
// abs() function returns the absolute (positive) value of a number:
echo abs(-4.5); // outputs 4.5
• sqrt()
// sqrt() function returns the square root of a number:
echo sqrt(81); // outputs 9
• round()
// round() function rounds a floating-point number to its nearest integer:
echo round(1.7); // outputs 2
echo round(1.3); // outputs 1
• floor()
// floor() function rounds a number DOWN to the nearest integer:
echo floor(2.4); // outputs 2
echo floor(3.8); // outputs 3
echo floor(-1.1); // outputs -2
• rand()
// rand() function generates a random number:
echo rand();
// Alo we can add the optional min and max parameters to specify the lowest integer
// and the highest integer to be returned.
// For instance, random integer between 1 and 10 (inclusive), use rand(10, 100):
echo rand(1, 10);
• count()
// count() function returns the number of elements in an array:
$countries = array("Georgia","Ukraine","Spain");
echo count($countries); // outputs 3
• pow()
// pow() function returns x raised to the power of y. ( x = 2 , y = 3):
echo pow(2,3); // outputs 8
• decbin()
// decbin() function converts decimal to binary:
echo decbin(2);// outputs 10
echo "\n";
echo decbin(10); // outputs 1010
strlen()
// function returns the length of a string.
echo strlen("Hello"); // outputs 5
• str_word_count()
// function counts the number of words in a string.
echo str_word_count("Hello world!"); // outputs 2
• strrev()
// function reverses a string.
echo strrev("Levani"); // outputs inaveL
• strpos()
/* function searches for a specific text within a string.
If a match is found, the function returns the character position of
the first match. If no match is found, it will return FALSE.*/
echo strpos("Hello world!", "l"); // outputs 2
// why ? index 0 = "H", index 1 = "e" and index 2 = "l"
• str_replace()
// function replaces some characters with some other characters in a string.
echo str_replace("l", "L", "levani"); // outputs Levani
• substr()
// function return part of a string between given indexes
echo substr("Hello world", 6)."\n"; // outputs world
echo substr("Hello world", 9, 2)."\n"; // outputs ld
echo substr("abcdef", -1)."\n"; // outputs f
echo substr("abcdef", -2)."\n"; // outputs ef
echo substr("abcdef", -3, 1)."\n"; // outputs d
• strtoupper()
// function make a string uppercase
echo strtoupper("i love coding")."\n"; // outputs I LOVE CODING
• strtolower()
// function make a string lowercase
echo strtolower("PHP")."\n"; // outputs php
• explode()
// function Split a string by a string
echo var_dump(explode(" ", "I am 19 years old"))."\n"; // outputs "I" "am" "19" ...
• implode()
// function join array elements with a string
echo implode(" ", ['Nika', 'Giorgi',"Saba"]); // outputs Nika Giorgi Saba
• trim()
// function strip whitespace (or other characters) from the beginning and end of a string
echo trim(" How are you ? ")."\n";
// outputs How are you ? (without any whitespace before and after the text)
$name = "Mads";
then we must need to use $name. $NAME will not work.
$name = "Mads";
echo $NAME; // output: error
If we defined function name in lowercase, but calling them in uppercase it will work. For instance, If we define function sum() {} then calling SUM() will also work.function sum () {
echo "10 + 10 = 20 \n";
}
SUM(); // output: 10 + 10 = 20
Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
For instance:// Equal
$x = 50;
$y = "50";
var_dump($x == $y); // returns TRUE because values are equal
echo "\n";
// Identical
$x = 50; // integer type
$y = "50"; // string type
var_dump($x === $y); // returns false because types are not equal
echo "\n";
// Not equal
$x = 50;
$y = "50";
var_dump($x != $y); // returns false because values are equal
echo "\n";
// Not equal. Both operators (!= / <>) give the same output. The only difference is that '<>' is in line with the ISO standard while '!= ' does not follow ISO standard.
$x = 50;
$y = "50";
var_dump($x <> $y); // returns false because values are equal
echo "\n";
// Not identical
$x = 50;
$y = "50";
var_dump($x !== $y); // returns true because types are not equal
echo "\n";
// Greater than
$x = 100;
$y = 50;
var_dump($x > $y); // returns true because $x is greater than $y
echo "\n";
// Less than
$x = 10;
$y = 50;
var_dump($x < $y); // returns true because $x is less than $y
echo "\n";
// Greater than or equal to
$x = 10;
$y = 10;
var_dump($x >= $y); // returns true because $x is greater than or equal to $y
echo "\n";
// Less than or equal to
$x = 10;
$y = 10;
var_dump($x <= $y); // returns true because $x is less than or equal to $y
echo "\n";
$x = 10;
$y = $x;
echo $y; // output: 10
Assignment Same as... Description
$x = $y $x = $y The left operand gets set to the value of the expression on the right
$x += $y $x = $x + $y Addition
$x -= $y $x = $x - $y Subtraction
$x *= $y $x = $x * $y Multiplication
$x /= $y $x = $x / $y Division
$x %= $y $x = $x % $y Modulus
For instance:
$x = 15;
$x %= 4; // output: 3
$x = 5;
$x *= 6; // output: 30
$x++; // equivalent to $x = $x+1;
$x--; // equivalent to $x = $x-1;
• Increment and decrement operators either precede or follow a variable.$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.
For instance:
$a = 2; $b = $a++; // $a=3, $b=2
$a = 2; $b = ++$a; // $a=3, $b=3
$a = 2; $b = $a--; // $a=1, $b=2
$a = 2; $b = --$a; // $a=1, $b=1