$array = [1, 2, "name"=>"Three", 4];
print_r($array);
ResultArray
(
[0] => 1
[1] => 2
[name] => Three
[2] => 4
)
If we set higher index manually, it will continue adding 1 to the max index$array = [1, 2, 7=>"Three", 4];
print_r($array);
ResultArray
(
[0] => 1
[1] => 2
[7] => Three
[8] => 4
)
If we set higher index manually, it will continue adding 1 to the index$array = [1, 2, 7=>"Three", 4=>"forth", 25];
print_r($array);
ResultArray
(
[0] => 1
[1] => 2
[7] => Three
[4] => forth
[8] => 25
)
Only integer and string values are allowed as array keys
$array = [
1 => "a",
"1" => "b",
"-1" => "-b",
'1.5' => "c",
true => "d",
];
print_r($array);
ResultArray
(
[1] => d
[-1] => -b
[1.5] => c
)
`
$items = array("me", "you", "he");
array_push($items, "she", "we");
print_r($items);
``
Formats can be:
echo "Today is ". date("Y/m/d") . "\n";
echo "Today is ". date("d/m/Y"). "\n";
echo "Today is ". date("D/M/Y"). "\n";
echo "Today is ". date("d/M/Y"). "\n";
echo "Today is ". date("d.m.Y"). "\n";
echo "Today is ". date("d-m-Y"). "\n";
echo "Today is ". date("l"). "\n";
`
`
Output:
Today is 2022/10/22
Today is 22/10/2022
Today is Sat/Oct/2022
Today is 22/Oct/2022
Today is 22.10.2022
Today is 22-10-2022
Today is Saturday
`<?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";
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// 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);