<?php
$colors = ["Red", 7735, "Blue"];
$colors[] = "Yellow"; //add new member in last position
echo $colors[0];
echo "\n\n\n";
print_r($colors);
echo "\n\n\n";
var_dump($colors);
OUTPUT:
Red
Array
(
[0] => Red
[1] => 7735
[2] => Blue
[3] => Yellow
)
array(4) {
[0]=>
string(3) "Red"
[1]=>
int(7735)
[2]=>
string(4) "Blue"
[3]=>
string(6) "Yellow"
}
function pyramid($n) {
// number of spaces
$k = $n;
// outer loop to handle number of rows
// n in this case
for ($i = 0; $i < $n; $i++) {
// inner loop to handle number spaces values changing acc.
// to requirement
for ($j = 0; $j < $k; $j++) {
echo " ";
}
// decrementing k after each loop
$k = $k - 1;
// inner loop to handle number of columns
// values changing acc. to outer loop
for ($j = 0; $j <= $i; $j++ )
{
// printing stars
echo "* ";
}
// ending line after each row
echo "\n";
}
}
// Driver Code
$n = 10;
pyramid($n);
Outputs:
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
gettype(variable);
For instance:
$int = 10;
echo gettype($int)."\n"; // outputs: integer
$str = "PHP";
echo gettype($str)."\n"; // outputs: string
$flt = 1.2;
echo gettype($flt)."\n"; // outputs: double (also called float)
$bool = true;
echo gettype($bool)."\n"; // outputs: boolean
$arr = [1,2,3,4,5,6,7,8,9,10];
echo gettype($arr)."\n"; // outputs: array
// in PHP
$array = [0, 1, 2];
$array2 = $array;
$array2[1] = 6;
array_push($array2, 3);
print_r ($array);
print_r($array2);
// $array = [0, 1, 2], $array2 = [0, 6, 2, 3]
// In JS
let array = [0, 1, 2];
let array2 = array;
array2[1] = 6;
array2.push(3);
console.log(array);
console.log(array2);
// array = array2 = [0, 6, 2, 3]
// in JS It's the same array (since it's an object, it's the same reference), you need to create a copy to manipulate them separately
// using slice() (or spread operator) in JS (which creates a new array with the elements at the first level copied over), like this:
let a = [0, 1, 2];
let b = a.slice();
b.push(3);
b[1] = 6;
console.log(a, b);
// and the result of 1st and 2nd arrays will not be same. like it wasn't in PHP
// P.s at that case will not change anything, because it's not reference already,
// we just took array's element, the primitive type (integer), not object or array.
let array = [0, 1, 2];
let value = array[1];
value = 10;
console.log(array); // [0, 1, 2], will not change
console.log(value); // 10
// but if there will be object, or array as an element in array and if we will modify on it,
// than the main array will change the value as well.
const array = [{name: 'lela'}, {name: 'gela'}, {name: 'bela'}];
const object = array[0];
object.name = 'test';
console.log(array);
console.log(object);
<?php
$arr=[];
for ($i=0; $i<10; $i++){
$arr[$i]= rand(0,100);
}
print_r($arr);
function str_info($string,$pos){
$arr= ["length is: ". strlen($string),
"UPPER:". strtoupper($string),
"string possitin: " .strpos($string, $pos),
"reverse is: ".strrev($string)];
return implode("\n", $arr) ; // แแฅ แจแแแแ แแแแก แแแกแแแแก แแแแแแแขแแแก แแ แแแแแ แฃแแแแก แขแแฅแกแขแแก แกแแฎแแ
แจแแแแแ
length is: 13
UPPER:YOU ARE GREAT
string possitin: 4
reverse is: taerg era uoY<?php
$x = 10;
// Sum of numbers from 1 to n
$sum = 0;
for ($i = 1; $i <= $x; $i++) {
$sum += $i;
}
echo "SUM of numbers from 1 to $x: $sum\n";
// easy way to calculate SUM of numbers to n
$sum = $from1To * ($x+1) / 2;
echo "SUM of numbers from 1 to $x: $sum\n";