-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHP Basic.txt
More file actions
1185 lines (893 loc) · 26.2 KB
/
Copy pathPHP Basic.txt
File metadata and controls
1185 lines (893 loc) · 26.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//print
<?php
$txt = "PHP";
echo "I love $txt!";
?>
//version cheek
echo phpversion();
//syntax
<?php
// PHP code goes here
?>
//simple php file
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
//PHP Case Sensitivity
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>"; // EcHo and echo treated differently
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
//$COLOR is not same as $color
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
//Comment
// This is a single-line comment
# This is also a single-line comment
/* This is a
multi-line comment */
//variable
$x = 5;//integer type
$y = "John";//string
//output of the variable inside text
$txt = "W3Schools.com";
echo "I love $txt!";
//summation of two value
$x = 5;
$y = 4;
echo $x + $y;
//Add two different variable of not same type
$x = 5; // $x is an integer
$y = "John"; // $y is a string
echo $x;
echo $y;
#cheek datatype of variable by var_dump() function
var_dump(5);//int
var_dump("John");//string
var_dump(3.14);//float
var_dump(true);//boolean
var_dump([2, 3, 56]);//array make in form of associative array
var_dump(NULL);//Null type
//Assign Multiple Values
$x = $y = $z = "Fruits"
$x = 5; // global scope
//Global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>"; //No output
}
myTest();//5
echo "<p>Variable x outside function is: $x</p>";
//Local scope
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";//5
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";//No output
echo "Hello";
//same as:
echo("Hello") but it is a function;//without parentheses
//Print by different styling way..
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
//Display text and varriable together
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
echo "<h2>$txt1</h2>";
echo "<p>Study PHP at $txt2</p>";
//array
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
//class sample basic
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . "!";
}
}
$myCar = new Car("red", "Volvo");
var_dump($myCar);
//single qotes and double
echo "Hello";
echo 'Hello';
//double qotes in text print
$x = "John";
echo "Hello $x";
//Length of string
echo strlen("Hello world!");===> strlen()
//Word count*
echo str_word_count("Hello world!");==> str_word_count()
//specific text within a string
echo strpos("Hello world!", "world");
//strtoupper()
$x = "Hello World!";
echo strtoupper($x);
//strtolower()
$x = "Hello World!";
echo strtolower($x);
//Replace String
$x = "Hello World!";
echo str_replace("World", "Dolly", $x);
//Reverse a String
$x = "Hello World!";
echo strrev($x);
//Remove Whitespace
$x = " Hello World! ";
echo trim($x);
Convert String into Array by explode() function
$x = "Hello World!";
$y = explode(" ", $x);
//Use the print_r() function to display the result:
print_r($y);
/*
Result:
Array ( [0] => Hello [1] => World! )
*/
//String Concatenation
$x = "Hello";
$y = "World";
$z = $x . " " . $y;
echo $z;
//Slicing string by substr function
$x = "Hello World!";
echo substr($x, 6, 5);
echo substr($x, 6);
echo substr($x, -5, 3);
echo substr($x, 5, -3);
//Escape character-> \
$x = "We are the so-called \"Vikings\" from the north.";
Example:-
\' Single Quote
\" Double Quote
\$ PHP variables
\n New Line
\r Carriage Return
\t Tab
\f Form Feed
\ooo Octal value
\xhh Hex value
//Check if the type of a variable is integer
$x = 5985;
var_dump(is_int($x));
$x = 59.85;
var_dump(is_int($x));
$x = 10.365;
var_dump(is_float($x));
$x = 1.9e411;
var_dump($x);
$x = acos(8);
var_dump($x);
//Check if the variable is numeric
$x = 5985;
var_dump(is_numeric($x));
$x = "5985";
var_dump(is_numeric($x));
$x = "59.85" + 100;
var_dump(is_numeric($x));
$x = "Hello";
var_dump(is_numeric($x));
//Cast float and string to integer typecasting
// Cast float to int
$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast;
echo "<br>";
// Cast string to int
$x = "23465.768";
$int_cast = (int)$x;
echo $int_cast;
//typecasting
$a = 5; // Integer
$b = 5.34; // Float
$c = "hello"; // String
$d = true; // Boolean
$e = NULL; // NULL
$a = (string) $a;
$b = (string) $b;
$c = (string) $c;
$d = (string) $d;
$e = (string) $e;
//To verify the type of any object in PHP, use the var_dump() function:
var_dump($a);
var_dump($b);
var_dump($c);
var_dump($d);
var_dump($e);
//pi() Function
echo(pi());
//min() and max() Functions
echo(min(0, 150, 30, 20, -8, -200));
echo(max(0, 150, 30, 20, -8, -200));
//abs function
echo(abs(-6.7));
//sqrt() Function
echo(sqrt(64));
//round() Function
echo(round(0.60));
echo(round(0.49));
//rand function
rand()->random number
echo(rand(10, 100));
//Constant operation by define()
define(name, value);
//Example
define("GREETING", "Coder Hub by Om!");
echo GREETING;
const MYCAR = "Volvo";
echo MYCAR;
//constant arrays
define("cars", [
"Alfa Romeo",
"BMW",
"Toyota"
]);
echo cars[0];
//As function
define("GREETING", "Coder Hub by Om!");
function myTest() {
echo GREETING;
}
myTest();
//Magic Constants
Constant Description
__CLASS__ If used inside a class, the class name is returned.
__DIR__ The directory of the file.
__FILE__ The file name including the full path.
__FUNCTION__ If inside a function, the function name is returned.
__LINE__ The current line number.
__METHOD__ If used inside a function that belongs to a class, both class and function name is returned.
__NAMESPACE__ If used inside a namespace, the name of the namespace is returned.
__TRAIT__ If used inside a trait, the trait name is returned.
ClassName::class Returns the name of the specified class and the name of the namespace, if any.
//PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
Operator Name Example Result Try it
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
** Exponentiation $x ** $y Result of raising $x to the $y'th pow
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
Assignment Same as... Description Try it
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
The PHP comparison operators are used to compare two values (number or string):
Operator Name Example Result Try it
== 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
<=> Spaceship $x <=> $y Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
Operator Same as... Description Try it
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
Operator Name Example Result Try it
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true
PHP String Operators
PHP has two operators that are specially designed for strings.
Operator Name Example Result Try it
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1
PHP Array Operators
The PHP array operators are used to compare arrays.
Operator Name Example Result Try it
+ Union $x + $y Union of $x and $y
== Equality $x == $y Returns true if $x and $y have the same key/value pairs
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types
!= Inequality $x != $y Returns true if $x is not equal to $y
<> Inequality $x <> $y Returns true if $x is not equal to $y
!== Non-identity $x !== $y Returns true if $x is not identical to $y
HP Conditional Assignment Operators
The PHP conditional assignment operators are used to set a value depending on conditions:
Operator Name Example Result Try it
?: Ternary $x = expr1 ? expr2 : expr3 Returns the value of $x.
The value of $x is expr2 if expr1 = TRUE.
The value of $x is expr3 if expr1 = FALSE
?? Null coalescing $x = expr1 ?? expr2 Returns the value of $x.
The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.
Introduced in PHP 7
PHP Conditional Statements
Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.
//In PHP we have the following conditional statements:
if statement - executes some code if one condition is true
if...else statement - executes some code if a condition is true and another code if that condition is false
if...elseif...else statement - executes different codes for more than two conditions
switch statement - selects one of many blocks of code to be executed
PHP - The if Statement
The if statement executes some code if one condition is true.
Syntax
if (condition) {
// code to be executed if condition is true;
}
//Example
if (5 > 3) {
echo "Have a good day!";
}
$t = 14;
if ($t < 20) {
echo "Have a good day!";
}
//Check if $t is equal to 14:
$t = 14;
if ($t == 14) {
echo "Have a good day!";
}
Logical Operators
To check more than one condition, we can use logical operators, like the && operator:
Example
//Check if $a is greater than $b, AND if $a is less than $c:
$a = 200;
$b = 33;
$c = 500;
if ($a > $b && $a < $c ) {
echo "Both conditions are true";
}
//Check if $a is either 2, 3, 4, 5, 6, or 7:
$a = 5;
if ($a == 2 || $a == 3 || $a == 4 || $a == 5 || $a == 6 || $a == 7) {
echo "$a is a number between 2 and 7";
}
//If and Else
PHP - The if...else Statement
The if...else statement executes some code if a condition is true and another code if that condition is false.
Syntax
if (condition) {
// code to be executed if condition is true;
} else {
// code to be executed if condition is false;
}
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
//PHP - The if...elseif...else Statement
The if...elseif...else statement executes different codes for more than two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
// code to be executed if first condition is false and this condition is true;
} else {
// code to be executed if all conditions are false;
}
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
//Short Hand If
$a = 5;
if ($a < 10) $b = "Hello";
echo $b
$a = 13;
$b = $a < 10 ? "Hello" : "Good Bye";
echo $b;
//Nested If
$a = 13;
if ($a > 10) {
echo "Above 10";
if ($a > 20) {
echo " and also above 20";
} else {
echo " but not above 20";
}
}
//The PHP switch Statement
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (expression) {
case label1:
//code block
break;
case label2:
//code block;
break;
case label3:
//code block
break;
default:
//code block
}
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
//PHP Loops
Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is true.
In PHP, we have the following loop types:
while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
Print $i as long as $i is less than 6:
$i = 1;
while ($i < 6) {
echo $i;
$i++;
}
$i = 1;
while ($i < 6) {
if ($i == 3) break;
echo $i;
$i++;
}
$i = 0;
while ($i < 6) {
$i++;
if ($i == 3) continue;
echo $i;
}
$i = 1;
while ($i < 6):
echo $i;
$i++;
endwhile;
$i = 0;
while ($i < 100) {
$i+=10;
echo $i "<br>";
}
$i = 1;
do {
echo $i;
$i++;
} while ($i < 6);
$i = 8;
do {
echo $i;
$i++;
} while ($i < 6);
$i = 0;
do {
$i++;
if ($i == 3) continue;
echo $i;
} while ($i < 6);
//The PHP for Loop
The for loop is used when you know how many times the script should run.
Syntax
for (expression1, expression2, expression3) {
// code block
}
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
for ($x = 0; $x <= 10; $x++) {
if ($x == 3) break;
echo "The number is: $x <br>";
}
for ($x = 0; $x <= 10; $x++) {
if ($x == 3) continue;
echo "The number is: $x <br>";
}
for ($x = 0; $x <= 100; $x+=10) {
echo "The number is: $x <br>";
}
//PHP foreach Loop
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
echo "$x <br>";
}
$members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach ($members as $x => $y) {
echo "$x : $y <br>";
}
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") break;
echo "$x <br>";
}
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") continue;
echo "$x <br>";
}$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") $x = "pink";
}
var_dump($colors);
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as &$x) {
if ($x == "blue") $x = "pink";
}
var_dump($colors);
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) :
echo "$x <br>";
endforeach;
//Break in For loop
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
$x = 0;
while($x < 10) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
$x++;
}
$i = 1;
do {
if ($i == 3) break;
echo $i;
$i++;
} while ($i < 6);
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") break;
echo "$x <br>";
}
//Continue in For Loops
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
}
$x = 0;
while($x < 10) {
if ($x == 4) {
continue;
}
echo "The number is: $x <br>";
$x++;
}
$i = 0;
do {
$i++;
if ($i == 3) continue;
echo $i;
} while ($i < 6);
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") continue;
echo "$x <br>";
}
//PHP Functions
function myMessage() {
echo "Hello world!";
}
function myMessage() {
echo "Hello world!";
}
myMessage();
function familyName($fname) {
echo "$fname Refsnes.<br>";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge")
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
function add_five(&$value) {
$value += 5;
}
$num = 2;
add_five($num);
echo $num;
function sumMyNumbers(...$x) {
$n = 0;
$len = count($x);
for($i = 0; $i < $len; $i++) {
$n += $x[$i];
}
return $n;
}
$a = sumMyNumbers(5, 2, 6, 2, 7, 7);
echo $a;
function myFamily($lastname, ...$firstname) {
$txt = "";
$len = count($firstname);
for($i = 0; $i < $len; $i++) {
$txt = $txt."Hi, $firstname[$i] $lastname.<br>";
}
return $txt;
}
$a = myFamily("Doe", "Jane", "John", "Joey");
echo $a;
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
//strict_types enabled
<?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : float {
return $a + $b;
}
echo addNumbers(1.2, 5.2);
?>
<?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : int {
return (int)($a + $b);
}
echo addNumbers(1.2, 5.2)
//Array to be continue...
$cars = array("Volvo", "BMW", "Toyota");
An array stores multiple values in one single variable..
PHP Array Types
In PHP, there are three types of arrays:
Indexed arrays - Arrays with a numeric index
Associative arrays - Arrays with named keys
Multidimensional arrays - Arrays containing one or more arrays
Array items of four different data types:
$myArr = array("Volvo", 15, ["apples", "bananas"], myFunction);
Array Functions:-
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
//PHP Indexed Arrays
$cars = array("Volvo", "BMW", "Toyota");
var_dump($cars);
//Access Indexed Arrays
$cars = array("Volvo", "BMW", "Toyota");
echo $cars[0];
//Change Value
$cars = array("Volvo", "BMW", "Toyota");
$cars[1] = "Ford";
var_dump($cars);
//Loop Through an Indexed Array
$cars = array("Volvo", "BMW", "Toyota");
foreach ($cars as $x) {
echo "$x <br>";
}
//PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them as a given value.
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
var_dump($car);
//Access Associative Arrays
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
echo $car["model"];
//Change Value
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
$car["year"] = 2024;
var_dump($car);
//Loop Through an Associative Array
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
foreach ($car as $x => $y) {
echo "$x: $y <br>";
}
//PHP Global Variables - Superglobals
Some predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.
The PHP superglobal variables are:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
//$_REQUEST
$_REQUEST is a PHP super global variable which contains submitted form data, and all cookie data.
In other words, $_REQUEST is an array containing data from $_GET, $_POST, and $_COOKIE.
You can access this data with the $_REQUEST keyword followed by the name of the form field, or cookie, like this:
$_REQUEST['firstname']
<html>
<body>
<form method="post" action="demo_request.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
</body>
</html>