// Make the array in JS
const array = [0, 1, 2];
array[4] = 4;
console.log(array[3]);
// outputs "undefined"
/* at the third index the array will get the value "undefined" because the value - 4 will be placed at the fourth index and the code will not skip the third index */
// Make the array in PHP
$arr = [0, 1, 2];
$arr[4] = 4;
echo $arr[3];
// unlike JS, that code will not returns anything, there is nothing in 3rd index.
// another interesting fact about it
console.log(array.length); // in JS
echo (count($arr)); // in PHP
// returns 5 in JS and 4 in PHP because in JS the 3rd index got the value - "undefined", but in PHP it just skipped the 3rd index and took the 4th position.// make the while loop example
$i = 1;
while ($i < 5) {
echo "the result is $i", "\n";
$i++;
}
// please note, If the condition never becomes false, the statement will continue to execute indefinitely.
// make the do...while loop
$i = 1;
do {
echo "hello world";
}
while ($i < 0);
// is that case will return the result?
/* Note that in a do while loop, the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements AT LEAST ONCE, EVEN IF THE CONDITION IS FALSE the first time */
// Therefore it returns "hello world" only one timeHello world
echo "Hello world";
Prints the two words one by oneecho "Hello ";
echo "world";
Prints the variable $greeting
$greeting = "Hello world";
Echo $greeting;
Concatenates the string to the variable $greeting
$greeting = "hello world!";
Echo $greeting . " Let's get started!";
const a = 10 + '20'; // the sum of integer and string values
const b = '20' + '20'; // the sum of two string values
console.log(a, b); // returns "1020" , "2020"
console.log(typeof(a, b)); // both are strings
$a = 10 + '20'; // The sum of an integer and a string
$b = '1020' + '20'; // The sum two strings
echo $a, "\n ", $b, "<br>"; // returns 30, 1040
echo var_dump($a, $b); // both are integers (int+str) (str+str)
/* When you try to perform math operations with strings in PHP, they are cast to approprite type. In this case to int. But you can cast integer back to string in the below example */
$integer = 10;
$stringIncrement = '20';
$result = $integer + $stringIncrement;
$result = (string) $result;
var_dump($result);
<?php declare(strict_types=1); // strict requirement
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is enabled and "5 days" is not an integer, an error will be thrown
?>
<!DOCTYPE html>
<html>
<body>
<?php
$students_inf = array(
array(
"Name" => 'Luka',
"Age" => 21,
"E-mail" => 'Luka.N111@gmail.com',
"Status" => 'Registered'
),
array(
"Name" => 'Nini',
"Age" => 20,
"E-mail" => 'Nini.K312@gmail.com',
"Status" => 'Unregistered'
),
array(
"Name" => 'Giorgi',
"Age" => 22,
"E-mail" => 'Giorgi.ES8@gmail.com',
"Status" => "Registered"
)
);
//You can get students information with echo and var_dump.
var_dump(json_encode($students_inf));
echo str_repeat("<br />",5);
echo $students_inf[0]['Name']. "<br />";
echo $students_inf[0]['Age']. "<br />";
echo $students_inf[1]['Status']. "<br />";
echo $students_inf[2]['E-mail']. "<br />";
?>
</body>
</html>
<?php
$name='GURAM';//Global variable
function local_var() {
$name='guram'; //local variable
echo 'Local variable: '.$name ."\n";
}
local_var();
echo 'Global variable: '.$name;
?>
"static"
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static keyword when you first declare the variable:
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>