\n
represents the newline (linefeed)
echo "Co\nding\n";
// output: Co
// ding
• \t
represents the tab
echo "Co\tding\n";
// output: Co ding
$arr = []; // or $arr = array();
$arr[0] = "Levani";
$arr[1] = "Nika";
$arr[2] = "Giorgi"; // or $arr = ["levani","Nika","Giorgi"]
$arr[] = "Saba"; // add last element in arr
echo "I am $arr[0] \n"; // output: I am Levani
• Associative array — An array where each key has its own specific value. In the following example the array uses keys instead of index numbers:
$ages = [
"John" => 15,
"Michael" => 25,
"Levani" => 19,
"Mads" => 57
];
// or
// $ages["John"] = "15";
// $ages["Michael"] = "25";
// $ages["Levani"] = "19";
// $ages["Mads"] = "57";
echo "I am ".$ages["Levani"]." years old \n"; // output: I am 19 years old
• Multidimensional array — An array containing one or more arrays within itself.
$laptop = [
"Lenovo" => [
"price" => 1200,
"color" => "white",
"storage" => "300 Gb",
],
"Dell" => [
"price" => 1000,
"color" => "black",
"storage" => "500 Gb",
],
];
echo "My laptop price is ".$laptop["Dell"]["price"].".\n"; // output: My laptop price is 1000.
<?php
$t = date("H");
if ($t < "18") {
echo "I watch TV";
} else {
echo "I play a game";
}
?>
<?php
$d = date("D");
if ($d == "Sun"){
echo "I don't work". ".";
}
?>
If the current hour is less than 20, the code will output "I am going to sleep"
<?php
$t = date("H");
if ($t < "22"){
echo " I am going to sleep";
}
?>
$str = "string"; // "hello" "Gamarjoba" 'levani' and so on.
• Integer
$int_num = 10; // -12 400 9999 -123421 90 and so on.
• Float
$float_num = 2.5; // -1.5 20.5 150.7 and so on.
• Boolean
$human = true; // equals 1
$robot = false; // equals 0
• Array
$laptops = array("Dell","Lenovo","Hp",1,2,3);
• constant
Constants are similar to variables except that they cannot be changed or undefined after they've been defined.
Begin the name of your constant with a letter or an underscore.
To create a constant, use the define() function. (no $ sign before the constant name):
• name: Specifies the name of the constant
• value: Specifies the value of the constant
• case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
define(name, value, case-insensitive)
// define constant
define("PI",3.141592);
echo PI;
I will discuss additional data types later, in a new note!echo
statement considered marginally faster than the print
statement since it doesn't return any value.
• print
always returns an integer value, which is 1.
$result = print 10;
echo $result; // output: 1
• Using print
, we cannot pass multiple arguments.
print 10,20,30; // error
echo
statement can be used to print the string,
multi-line strings, escaping characters, variable, array, etc.
• printing string
echo "Hello friends!\n";
• printing multi line string
echo "How are you ?
this is multi line
text !\n";
• printing escaping characters
echo "Hello \"Hello\" Hello\n";
• printing variable value ( only DOUBLE quetes can parse variables )
$num= 17;
echo "num = $num\n";
// or => echo "num = ".$num."\n";
// or => echo 'num = '.$num."\n";
• printing number without quotes
echo 10;
• printing multiple parameters
echo "red"," yellow"," black";
• echo can be used with or without parentheses: echo(), and echo.
echo ("function\n");
• Text inside double quotes can include single quotes as part of the text.
The opposite is also possible.
echo "My name is 'Anton'\n";
echo 'My name is "Anton"';
➤ PHP print
statement works exactly the same way except
that the print statement can only output one parameter, and always returns 1.
• returns 1
$result = print"Hello\n";
echo "result = $result"; // output: 1
• print
can take only one argument
print "hello","world"; // error
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$a = 2 /* - 1 */ + 4;
<?php
// Math functions
# Function min() finds the lowest value from its arguments, example:
echo min(21, 78, 20, 47, 89). "\n"; //outputs - 20;
# Function max() finds the highest value from its arguments, example:
echo max(78, 97, 109, 47, 445). "\n"; //outputs - 445;
# Function count() counts all elements in the array, example:
echo count(67, 89, 76, 54, 9, 90). "\n"; //outputs - 6;
# Function round() rounds a floating-point number to its nearest integer, example:
echo(round(9.43)). "\n"; //outputs - 9;
echo(round(5.5)). "\n"; //outputs - 6;
# Function floor() rounds the floating-point number down, example:
echo floor(9.7). "\n"; //outputs - 9;
echo floor(6.1). "\n"; //outputs - 6;
# Function ceil() rounds the floating-point number up, example:
echo ceil(9.8). "\n"; //outputs - 10;
echo ceil(7.2). "\n"; //outputs - 8;
# Function rand() generates a random number, example:
echo rand(9, 88). "\n"; //outputs some number from 9 to 88;
# Function sqrt() returns the square root of a number, example:
echo sqrt(81). "\n"; //outputs - 9;
# Function pow() raises 2 to the power of 3, example:
echo pow(2, 3). "\n"; //output 8;
# Function pi() Returns PI value, example:
echo pi(). "\n"; //outputs - 3.1415926535898
# Function abs() returns the positive (absolute) value of a number, example:
echo abs(-87). "\n"; //outputs - 87;
# Function decbin() converts decimal number into binary, example:
echo decbin(44). "\n"; //outputs - 101100;
?>