-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp.html
More file actions
8136 lines (5914 loc) · 325 KB
/
Copy pathphp.html
File metadata and controls
8136 lines (5914 loc) · 325 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
<!DOCTYPE html><html><body><title>PHP Tutorial</title>
<h1>PHP Tutorial</h1>
<p>The PHP Hypertext Preprocessor (PHP) is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications. This tutorial helps you to build your base with PHP.</p>
<h1>Audience</h1>
<p>This tutorial is designed for PHP programmers who are completely unaware of PHP concepts but they have basic understanding on computer programming.</p>
<h1>Prerequisites</h1>
<p>Before proceeding with this tutorial you should have at least basic understanding of computer programming, Internet, Database, and MySQL etc is very helpful.</p>
<h1>PHP Offline Mobile App</h1>
<h1>Execute PHP Online</h1>
<p>For most of the examples given in this tutorial you will find <b>Try it</b> an option, so just make use of this option to execute your PHP programs at the spot and enjoy your learning.</p>
<p>Try following example using <b>Try it</b> option available at the top right corner of the below sample code box −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
echo "<h1>Hello, PHP!</h1>";
?>
</body>
</html>
</pre>
<title>PHP Introduction</title>
<h1>PHP - Introduction</h1>
<p>PHP started out as a small open source project that evolved as more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994.</p>
<p>PHP is a recursive acronym for "PHP: Hypertext Preprocessor".</p>
<p>PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.</p>
<p>It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.</p>
<p>PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server, once started, executes even very complex queries with huge result sets in record-setting time.</p>
<p>PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added support for Java and distributed object architectures (COM and CORBA), making n-tier development a possibility for the first time.</p>
<p>PHP is forgiving: PHP language tries to be as forgiving as possible.</p>
<p>PHP Syntax is C-Like.</p>
<h2>Common uses of PHP</h2>
<p>PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.</p>
<p>PHP can handle forms, i.e. gather data from files, save data to a file, through email you can send data, return data to the user.</p>
<p>You add, delete, modify elements within your database through PHP.</p>
<p>Access cookies variables and set cookies.</p>
<p>Using PHP, you can restrict users to access some pages of your website.</p>
<p>It can encrypt data.</p>
<h2>Characteristics of PHP</h2>
<p>Five important characteristics make PHP's practical nature possible −</p>
<h2>"Hello World" Script in PHP</h2>
<p>To get a feel for PHP, first start with simple PHP scripts. Since "Hello, World!" is an essential example, first we will create a friendly little "Hello, World!" script.</p>
<p>As mentioned earlier, PHP is embedded in HTML. That means that in amongst your normal HTML (or XHTML if you're cutting-edge) you'll have PHP statements like this −</p>
<p>It will produce following result −</p>
<p>If you examine the HTML output of the above example, you'll notice that the PHP code is not present in the file sent from the server to your Web browser. All of the PHP present in the Web page is processed and stripped from the page; the only thing returned to the client from the Web server is pure HTML output.</p>
<p>All PHP code must be included inside one of the three special markup tags ate are recognised by the PHP Parser.</p>
<p>A most common tag is the <?php...?> and we will also use the same tag in our tutorial.</p>
<p>From the next chapter we will start with PHP Environment Setup on your machine and then we will dig out almost all concepts related to PHP to make you comfortable with the PHP language.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php echo "Hello, World!";?>
</body>
</html>
</pre>
<p>It will produce following result −</p>
<pre class="result notranslate">
Hello, World!
</pre>
<p>If you examine the HTML output of the above example, you'll notice that the PHP code is not present in the file sent from the server to your Web browser. All of the PHP present in the Web page is processed and stripped from the page; the only thing returned to the client from the Web server is pure HTML output.</p>
<p>All PHP code must be included inside one of the three special markup tags ate are recognised by the PHP Parser.</p>
<pre class="prettyprint notranslate">
<?php PHP code goes here ?>
<? PHP code goes here ?>
<script language="php"> PHP code goes here </script>
</pre>
<title>PHP Environment Setup Windows, Linux and Mac OS X</title>
<h1>PHP - Environment Setup</h1>
<h2>Try it Option Online</h2>
<p>We have set up the PHP Programming environment on-line, so that you can compile and execute all the available examples on line. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it on-line.</p>
<p>Try the following example using our on-line compiler available at <a href="http://www.tutorialspoint.com/codingground.htm" target="_blank" > CodingGround.</a></p>
<p>For most of the examples given in this tutorial, you will find a <b>Try it</b> option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning.</p>
<p>In order to develop and run PHP Web pages three vital components need to be installed on your computer system.</p>
<p><b>Web Server</b> − PHP will work with virtually all Web Server software, including Microsoft's Internet Information Server (IIS) but then most often used is freely available Apache Server. Download Apache for free here − <a rel="nofollow" href="https://httpd.apache.org/download.cgi" target="_blank">https://httpd.apache.org/download.cgi</a></p>
<p><b>Database</b> − PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database. Download MySQL for free here − <a rel="nofollow" target="_blank" href="https://www.mysql.com/downloads/">https://www.mysql.com/downloads/</a></p>
<p><b>PHP Parser</b> − In order to process PHP script instructions a parser must be installed to generate HTML output that can be sent to the Web Browser. This tutorial will guide you how to install PHP parser on your computer.</p>
<h2>PHP Parser Installation</h2>
<p>Before you proceed it is important to make sure that you have proper environment setup on your machine to develop your web programs using PHP.</p>
<p>Type the following address into your browser's address box.</p>
<p>If this displays a page showing your PHP installation related information then it means you have PHP and Webserver installed properly. Otherwise you have to follow given procedure to install PHP on your computer.</p>
<p>This section will guide you to install and configure PHP over the following four platforms −</p>
<p><a href="/php/php_installation_linux.htm">PHP Installation on Linux or Unix with Apache</a></p>
<p><a href="/php/php_installation_mac.htm">PHP Installation on Mac OS X with Apache</a></p>
<p><a href="/php/php_installation_windows_iis.htm">PHP Installation on Windows NT/2000/XP with IIS</a></p>
<p><a href="/php/php_installation_windows_apache.htm">PHP Installation on Windows NT/2000/XP with Apache</a></p>
<h3>Apache Configuration</h3>
<p>If you are using Apache as a Web Server then this section will guide you to edit Apache Configuration Files.</p>
<p>Just Check it here − <a href="/php/php_apache_configuration.htm">PHP Configuration in Apache Server</a></p>
<h3>PHP.INI File Configuration</h3>
<p>The PHP configuration file, php.ini, is the final and most immediate way to affect PHP's functionality.</p>
<p>Just Check it here − <a href="/php/php_ini_configuration.htm">PHP.INI File Configuration</a></p>
<h3>Windows IIS Configuration</h3>
<p>To configure IIS on your Windows machine you can refer your IIS Reference Manual shipped along with IIS.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate tryit">
<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
echo "<h1>Hello, PHP!</h1>";
?>
</body>
</html>
</pre>
<p>For most of the examples given in this tutorial, you will find a <b>Try it</b> option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning.</p>
</blockquote>
<p>In order to develop and run PHP Web pages three vital components need to be installed on your computer system.</p>
<ul class="list">
<li><p><b>Web Server</b> − PHP will work with virtually all Web Server software, including Microsoft's Internet Information Server (IIS) but then most often used is freely available Apache Server. Download Apache for free here − <a rel="nofollow" href="https://httpd.apache.org/download.cgi" target="_blank">https://httpd.apache.org/download.cgi</a></p></li>
<li><p><b>Database</b> − PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database. Download MySQL for free here − <a rel="nofollow" target="_blank" href="https://www.mysql.com/downloads/">https://www.mysql.com/downloads/</a></p></li>
<li><p><b>PHP Parser</b> − In order to process PHP script instructions a parser must be installed to generate HTML output that can be sent to the Web Browser. This tutorial will guide you how to install PHP parser on your computer.</p></li>
</ul>
<h2>PHP Parser Installation</h2>
<p>Before you proceed it is important to make sure that you have proper environment setup on your machine to develop your web programs using PHP.</p>
<p>Type the following address into your browser's address box.</p>
<pre class="result notranslate">
http://127.0.0.1/info.php
</pre>
<title>PHP Syntax Overview</title>
<h1>PHP - Syntax Overview</h1>
<p>This chapter will give you an idea of very basic syntax of PHP and very important to make your PHP foundation strong.</p>
<h2>Escaping to PHP</h2>
<p>The PHP parsing engine needs a way to differentiate PHP code from other elements in the page. The mechanism for doing so is known as 'escaping to PHP'. There are four ways to do this −</p>
<h3>Canonical PHP tags</h3>
<p>The most universally effective PHP tag style is −</p>
<p>If you use this style, you can be positive that your tags will always be correctly interpreted.</p>
<h3>Short-open (SGML-style) tags</h3>
<p>Short or short-open tags look like this −</p>
<p>Short tags are, as one might expect, the shortest option You must do one of two things to enable PHP to recognize the tags −</p>
<p>Choose the --enable-short-tags configuration option when you're building PHP.</p>
<p>Set the short_open_tag setting in your php.ini file to on. This option must be disabled to parse XML with PHP because the same syntax is used for XML tags.</p>
<h3>ASP-style tags</h3>
<p>ASP-style tags mimic the tags used by Active Server Pages to delineate code blocks. ASP-style tags look like this −</p>
<p>To use ASP-style tags, you will need to set the configuration option in your php.ini file.</p>
<h3>HTML script tags</h3>
<p>HTML script tags look like this −</p>
<h2>Commenting PHP Code</h2>
<p>A <i>comment</i> is the portion of a program that exists only for the human reader and stripped out before displaying the programs result. There are two commenting formats in PHP −</p>
<p><b>Single-line comments</b> − They are generally used for short explanations or notes relevant to the local code. Here are the examples of single line comments.</p>
<p><b>Multi-lines printing</b> − Here are the examples to print multiple lines in a single print statement −</p>
<p><b>Multi-lines comments</b> − They are generally used to provide pseudocode algorithms and more detailed explanations when necessary. The multiline style of commenting is the same as in C. Here are the example of multi lines comments.</p>
<h2>PHP is whitespace insensitive</h2>
<p>Whitespace is the stuff you type that is typically invisible on the screen, including spaces, tabs, and carriage returns (end-of-line characters).</p>
<p>PHP whitespace insensitive means that it almost never matters how many whitespace characters you have in a row.one whitespace character is the same as many such characters.</p>
<p>For example, each of the following PHP statements that assigns the sum of 2 + 2 to the variable $four is equivalent −</p>
<h2>PHP is case sensitive</h2>
<p>Yeah it is true that PHP is a case sensitive language. Try out following example −</p>
<p>This will produce the following result −</p>
<h2>Statements are expressions terminated by semicolons</h2>
<p>A <i>statement</i> in PHP is any expression that is followed by a semicolon (;).Any sequence of valid PHP statements that is enclosed by the PHP tags is a valid PHP program. Here is a typical statement in PHP, which in this case assigns a string of characters to a variable called $greeting −</p>
<h2>Expressions are combinations of tokens</h2>
<p>The smallest building blocks of PHP are the indivisible tokens, such as numbers (3.14159), strings (.two.), variables ($two), constants (TRUE), and the special words that make up the syntax of PHP itself like if, else, while, for and so forth</p>
<h2>Braces make blocks</h2>
<p>Although statements cannot be combined like expressions, you can always put a sequence of statements anywhere a statement can go by enclosing them in a set of curly braces.</p>
<p>Here both statements are equivalent −</p>
<h2>Running PHP Script from Command Prompt</h2>
<p>Yes you can run your PHP script on your command prompt. Assuming you have following content in test.php file</p>
<p>Now run this script as command prompt as follows −</p>
<p>It will produce the following result −</p>
<p>Hope now you have basic knowledge of PHP Syntax.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
<?php...?>
</pre>
<p>If you use this style, you can be positive that your tags will always be correctly interpreted.</p>
<h3>Short-open (SGML-style) tags</h3>
<p>Short or short-open tags look like this −</p>
<pre class="result notranslate">
<?...?>
</pre>
<p>Short tags are, as one might expect, the shortest option You must do one of two things to enable PHP to recognize the tags −</p>
<ul class="list">
<li><p>Choose the --enable-short-tags configuration option when you're building PHP.</p></li>
<li><p>Set the short_open_tag setting in your php.ini file to on. This option must be disabled to parse XML with PHP because the same syntax is used for XML tags.</p></li>
</ul>
<h3>ASP-style tags</h3>
<p>ASP-style tags mimic the tags used by Active Server Pages to delineate code blocks. ASP-style tags look like this −</p>
<pre class="result notranslate">
<%...%>
</pre>
<p>To use ASP-style tags, you will need to set the configuration option in your php.ini file.</p>
<h3>HTML script tags</h3>
<p>HTML script tags look like this −</p>
<pre class="prettyprint notranslate">
<script language="PHP">...</script>
</pre>
<h2>Commenting PHP Code</h2>
<p>A <i>comment</i> is the portion of a program that exists only for the human reader and stripped out before displaying the programs result. There are two commenting formats in PHP −</p>
<p><b>Single-line comments</b> − They are generally used for short explanations or notes relevant to the local code. Here are the examples of single line comments.</p>
<pre class="prettyprint notranslate">
<?
# This is a comment, and
# This is the second line of the comment
// This is a comment too. Each style comments only
print "An example with single line comments";
?>
</pre>
<p><b>Multi-lines printing</b> − Here are the examples to print multiple lines in a single print statement −</p>
<pre class="prettyprint notranslate">
<?
# First Example
print <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
END;
# Second Example
print "This spans
multiple lines. The newlines will be
output as well";
?>
</pre>
<p><b>Multi-lines comments</b> − They are generally used to provide pseudocode algorithms and more detailed explanations when necessary. The multiline style of commenting is the same as in C. Here are the example of multi lines comments.</p>
<pre class="prettyprint notranslate">
<?
/* This is a comment with multiline
Author : Mohammad Mohtashim
Purpose: Multiline Comments Demo
Subject: PHP
*/
print "An example with multi line comments";
?>
</pre>
<h2>PHP is whitespace insensitive</h2>
<p>Whitespace is the stuff you type that is typically invisible on the screen, including spaces, tabs, and carriage returns (end-of-line characters).</p>
<p>PHP whitespace insensitive means that it almost never matters how many whitespace characters you have in a row.one whitespace character is the same as many such characters.</p>
<p>For example, each of the following PHP statements that assigns the sum of 2 + 2 to the variable $four is equivalent −</p>
<pre class="prettyprint notranslate">
$four = 2 + 2; // single spaces
$four <tab>=<tab2<tab>+<tab>2 ; // spaces and tabs
$four =
2+
2; // multiple lines
</pre>
<h2>PHP is case sensitive</h2>
<p>Yeah it is true that PHP is a case sensitive language. Try out following example −</p>
<pre class="prettyprint notranslate tryit">
<html>
<body>
<?php
$capital = 67;
print("Variable capital is $capital<br>");
print("Variable CaPiTaL is $CaPiTaL<br>");
?>
</body>
</html>
</pre>
<p>This will produce the following result −</p>
<pre class="result notranslate">
Variable capital is 67
Variable CaPiTaL is
</pre>
<h2>Statements are expressions terminated by semicolons</h2>
<p>A <i>statement</i> in PHP is any expression that is followed by a semicolon (;).Any sequence of valid PHP statements that is enclosed by the PHP tags is a valid PHP program. Here is a typical statement in PHP, which in this case assigns a string of characters to a variable called $greeting −</p>
<pre class="result notranslate">
$greeting = "Welcome to PHP!";
</pre>
<h2>Expressions are combinations of tokens</h2>
<p>The smallest building blocks of PHP are the indivisible tokens, such as numbers (3.14159), strings (.two.), variables ($two), constants (TRUE), and the special words that make up the syntax of PHP itself like if, else, while, for and so forth</p>
<h2>Braces make blocks</h2>
<p>Although statements cannot be combined like expressions, you can always put a sequence of statements anywhere a statement can go by enclosing them in a set of curly braces.</p>
<p>Here both statements are equivalent −</p>
<pre class="prettyprint notranslate">
if (3 == 2 + 1)
print("Good - I haven't totally lost my mind.<br>");
if (3 == 2 + 1) {
print("Good - I haven't totally");
print("lost my mind.<br>");
}
</pre>
<h2>Running PHP Script from Command Prompt</h2>
<p>Yes you can run your PHP script on your command prompt. Assuming you have following content in test.php file</p>
<pre class="prettyprint notranslate tryit">
<?php
echo "Hello PHP!!!!!";
?>
</pre>
<p>Now run this script as command prompt as follows −</p>
<pre class="result notranslate">
$ php test.php
</pre>
<p>It will produce the following result −</p>
<pre class="result notranslate">
Hello PHP!!!!!
</pre>
<title>PHP Variable Types</title>
<h1>PHP - Variable Types</h1>
<p>The main way to store information in the middle of a PHP program is by using a variable.</p>
<p>Here are the most important things to know about variables in PHP.</p>
<p>All variables in PHP are denoted with a leading dollar sign ($).</p>
<p>The value of a variable is the value of its most recent assignment.</p>
<p>Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.</p>
<p>Variables can, but do not need, to be declared before assignment.</p>
<p>Variables in PHP do not have intrinsic types - a variable does not know in advance whether it will be used to store a number or a string of characters.</p>
<p>Variables used before they are assigned have default values.</p>
<p>PHP does a good job of automatically converting types from one to another when necessary.</p>
<p>PHP variables are Perl-like.</p>
<p>PHP has a total of eight data types which we use to construct our variables −</p>
<p><b>Integers</b> − are whole numbers, without a decimal point, like 4195.</p>
<p><b>Doubles</b> − are floating-point numbers, like 3.14159 or 49.1.</p>
<p><b>Booleans</b> − have only two possible values either true or false.</p>
<p><b>NULL</b> − is a special type that only has one value: NULL.</p>
<p><b>Strings</b> − are sequences of characters, like 'PHP supports string operations.'</p>
<p><b>Arrays</b> − are named and indexed collections of other values.</p>
<p><b>Objects</b> − are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.</p>
<p><b>Resources</b> − are special variables that hold references to resources external to PHP (such as database connections).</p>
<p>The first five are <i>simple types</i>, and the next two (arrays and objects) are compound - the compound types can package up other arbitrary values of arbitrary type, whereas the simple types cannot.</p>
<p>We will explain only simple data type in this chapters. Array and Objects will be explained separately.</p>
<h2>Integers</h2>
<p>They are whole numbers, without a decimal point, like 4195. They are the simplest type .they correspond to simple whole numbers, both positive and negative. Integers can be assigned to variables, or they can be used in expressions, like so −</p>
<p>Integer can be in decimal (base 10), octal (base 8), and hexadecimal (base 16) format. Decimal format is the default, octal integers are specified with a leading 0, and hexadecimals have a leading 0x.</p>
<p>For most common platforms, the largest integer is (2**31 . 1) (or 2,147,483,647), and the smallest (most negative) integer is . (2**31 . 1) (or .2,147,483,647).</p>
<h2>Doubles</h2>
<p>They like 3.14159 or 49.1. By default, doubles print with the minimum number of decimal places needed. For example, the code −</p>
<p>It produces the following browser output −</p>
<h2>Boolean</h2>
<p>They have only two possible values either true or false. PHP provides a couple of constants especially for use as Booleans: TRUE and FALSE, which can be used like so −</p>
<h3>Interpreting other types as Booleans</h3>
<p>Here are the rules for determine the "truth" of any value not already of the Boolean type −</p>
<p>If the value is a number, it is false if exactly equal to zero and true otherwise.</p>
<p>If the value is a string, it is false if the string is empty (has zero characters) or is the string "0", and is true otherwise.</p>
<p>Values of type NULL are always false.</p>
<p>If the value is an array, it is false if it contains no other values, and it is true otherwise. For an object, containing a value means having a member variable that has been assigned a value.</p>
<p>Valid resources are true (although some functions that return resources when they are successful will return FALSE when unsuccessful).</p>
<p>Don't use double as Booleans.</p>
<p>Each of the following variables has the truth value embedded in its name when it is used in a Boolean context.</p>
<h2>NULL</h2>
<p>NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this −</p>
<p>The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed −</p>
<p>A variable that has been assigned NULL has the following properties −</p>
<p>It evaluates to FALSE in a Boolean context.</p>
<p>It returns FALSE when tested with IsSet() function.</p>
<h2>Strings</h2>
<p>They are sequences of characters, like "PHP supports string operations". Following are valid examples of string</p>
<p>Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.</p>
<p>This will produce following result −</p>
<p>There are no artificial limits on string length - within the bounds of available memory, you ought to be able to make arbitrarily long strings.</p>
<p>Strings that are delimited by double quotes (as in "this") are preprocessed in both the following two ways by PHP −</p>
<p>Certain character sequences beginning with backslash (\) are replaced with special characters</p>
<p>Variable names (starting with $) are replaced with string representations of their values.</p>
<p>The escape-sequence replacements are −</p>
<h3>Here Document</h3>
<p>You can assign multiple lines to a single string variable using here document −</p>
<p>This will produce following result −</p>
<h2>Variable Scope</h2>
<p>Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types −</p>
<p><a href="/php/php_local_variables.htm">Local variables</a></p>
<p><a href="/php/php_function_parameters.htm">Function parameters</a></p>
<p><a href="/php/php_global_variables.htm">Global variables</a></p>
<p><a href="/php/php_static_variables.htm">Static variables</a></p>
<h2>Variable Naming</h2>
<p>Rules for naming a variable is −</p>
<p>Variable names must begin with a letter or underscore character.</p>
<p>A variable name can consist of numbers, letters, underscores but you cannot use characters like + , - , % , ( , ) . & , etc</p>
<p>There is no size limit for variables.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
$int_var = 12345;
$another_int = -12345 + 12345;
</pre>
<p>Integer can be in decimal (base 10), octal (base 8), and hexadecimal (base 16) format. Decimal format is the default, octal integers are specified with a leading 0, and hexadecimals have a leading 0x.</p>
<p>For most common platforms, the largest integer is (2**31 . 1) (or 2,147,483,647), and the smallest (most negative) integer is . (2**31 . 1) (or .2,147,483,647).</p>
<h2>Doubles</h2>
<p>They like 3.14159 or 49.1. By default, doubles print with the minimum number of decimal places needed. For example, the code −</p>
<pre class="prettyprint notranslate tryit">
<?php
$many = 2.2888800;
$many_2 = 2.2111200;
$few = $many + $many_2;
print("$many + $many_2 = $few <br>");
?>
</pre>
<p>It produces the following browser output −</p>
<pre class="result notranslate">
2.28888 + 2.21112 = 4.5
</pre>
<h2>Boolean</h2>
<p>They have only two possible values either true or false. PHP provides a couple of constants especially for use as Booleans: TRUE and FALSE, which can be used like so −</p>
<pre class="prettyprint notranslate">
if (TRUE)
print("This will always print<br>");
else
print("This will never print<br>");
</pre>
<h3>Interpreting other types as Booleans</h3>
<p>Here are the rules for determine the "truth" of any value not already of the Boolean type −</p>
<ul class="list">
<li><p>If the value is a number, it is false if exactly equal to zero and true otherwise.</p></li>
<li><p>If the value is a string, it is false if the string is empty (has zero characters) or is the string "0", and is true otherwise.</p></li>
<li><p>Values of type NULL are always false.</p></li>
<li><p>If the value is an array, it is false if it contains no other values, and it is true otherwise. For an object, containing a value means having a member variable that has been assigned a value.</p></li>
<li><p>Valid resources are true (although some functions that return resources when they are successful will return FALSE when unsuccessful).</p></li>
<li><p>Don't use double as Booleans.</p></li>
</ul>
<p>Each of the following variables has the truth value embedded in its name when it is used in a Boolean context.</p>
<pre class="prettyprint notranslate">
$true_num = 3 + 0.14159;
$true_str = "Tried and true"
$true_array[49] = "An array element";
$false_array = array();
$false_null = NULL;
$false_num = 999 - 999;
$false_str = "";
</pre>
<h2>NULL</h2>
<p>NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this −</p>
<pre class="prettyprint notranslate">
$my_var = NULL;
</pre>
<p>The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed −</p>
<pre class="prettyprint notranslate">
$my_var = null;
</pre>
<p>A variable that has been assigned NULL has the following properties −</p>
<ul class="list">
<li><p>It evaluates to FALSE in a Boolean context.</p></li>
<li><p>It returns FALSE when tested with IsSet() function.</p></li>
</ul>
<h2>Strings</h2>
<p>They are sequences of characters, like "PHP supports string operations". Following are valid examples of string</p>
<pre class="prettyprint notranslate">
$string_1 = "This is a string in double quotes";
$string_2 = 'This is a somewhat longer, singly quoted string';
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters
</pre>
<p>Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.</p>
<pre class="prettyprint notranslate tryit">
<?php
$variable = "name";
$literally = 'My $variable will not print!';
print($literally);
print "<br>";
$literally = "My $variable will print!";
print($literally);
?>
</pre>
<p>This will produce following result −</p>
<pre class="result notranslate">
My $variable will not print!\n
My name will print
</pre>
<p>There are no artificial limits on string length - within the bounds of available memory, you ought to be able to make arbitrarily long strings.</p>
<p>Strings that are delimited by double quotes (as in "this") are preprocessed in both the following two ways by PHP −</p>
<ul class="list">
<li><p>Certain character sequences beginning with backslash (\) are replaced with special characters</p></li>
<li><p>Variable names (starting with $) are replaced with string representations of their values.</p></li>
</ul>
<p>The escape-sequence replacements are −</p>
<ul class="list">
<li>\n is replaced by the newline character</li>
<li>\r is replaced by the carriage-return character</li>
<li>\t is replaced by the tab character</li>
<li>\$ is replaced by the dollar sign itself ($)</li>
<li>\" is replaced by a single double-quote (")</li>
<li>\\ is replaced by a single backslash (\)</li>
</ul>
<h3>Here Document</h3>
<p>You can assign multiple lines to a single string variable using here document −</p>
<pre class="prettyprint notranslate tryit">
<?php
$channel =<<<_XML_
<channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/ </link>
<description>Choose what to eat tonight.</description>
</channel>
_XML_;
echo <<<END
This uses the "here document" syntax to output multiple lines with variable
interpolation. Note that the here document terminator must appear on a line with
just a semicolon. no extra whitespace!
<br />
END;
print $channel;
?>
</pre>
<p>This will produce following result −</p>
<pre class="result notranslate">
This uses the "here document" syntax to output
multiple lines with variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
<channel>
<title>What's For Dinner<title>
<link>http://menu.example.com/<link>
<description>Choose what to eat tonight.</description>
</pre>
<title>PHP Constants Types</title>
<h1>PHP - Constants Types</h1>
<p>A constant is a name or an identifier for a simple value. A constant value cannot change during the execution of the script. By default, a constant is case-sensitive. By convention, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.</p>
<p>To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $. You can also use the function constant() to read a constant's value if you wish to obtain the constant's name dynamically.</p>
<h2>constant() function</h2>
<p>As indicated by the name, this function will return the value of the constant.</p>
<p>This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function.</p>
<h2>constant() example</h2>
<p>Only scalar data (boolean, integer, float and string) can be contained in constants.</p>
<h2>Differences between constants and variables are</h2>
<p>There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.</p>
<p>Constants cannot be defined by simple assignment, they may only be defined using the define() function.</p>
<p>Constants may be defined and accessed anywhere without regard to variable scoping rules.</p>
<p>Once the Constants have been set, may not be redefined or undefined.</p>
<h2>Valid and invalid constant names</h2>
<h2>PHP Magic constants</h2>
<p>PHP provides a large number of predefined constants to any script which it runs.</p>
<p>There are five magical constants that change depending on where they are used. For example, the value of __LINE__ depends on the line that it's used on in your script. These special constants are case-insensitive and are as follows −</p>
<p>A few "magical" PHP constants are given below −</p>
<p><b>__LINE__</b></p>
<p>The current line number of the file.</p>
<p><b>__FILE__</b></p>
<p>The full path and filename of the file. If used inside an include,the name of the included file is returned. Since PHP 4.0.2, <b>__FILE__</b> always contains an absolute path whereas in older versions it contained relative path under some circumstances.</p>
<p><b>__FUNCTION__</b></p>
<p>The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.</p>
<p><b>__CLASS__</b></p>
<p>The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.</p>
<p><b>__METHOD__</b></p>
<p>The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="prettyprint notranslate">
<?php
define("MINSIZE", 50);
echo MINSIZE;
echo constant("MINSIZE"); // same thing as the previous line
?>
</pre>
<p>Only scalar data (boolean, integer, float and string) can be contained in constants.</p>
<h2>Differences between constants and variables are</h2>
<ul class="list">
<li><p>There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.</p></li>
<li><p>Constants cannot be defined by simple assignment, they may only be defined using the define() function.</p></li>
<li><p>Constants may be defined and accessed anywhere without regard to variable scoping rules.</p></li>
<li><p>Once the Constants have been set, may not be redefined or undefined.</p></li>
</ul>
<h2>Valid and invalid constant names</h2>
<pre class="result notranslate">
// Valid constant names
define("ONE", "first thing");
define("TWO2", "second thing");
define("THREE_3", "third thing");
// Invalid constant names
define("2TWO", "second thing");
define("__THREE__", "third value");
</pre>
<title>PHP Operator Types</title>
<h1>PHP - Operator Types</h1>
<p><b>What is Operator?</b> Simple answer can be given using expression <i>4 + 5 is equal to 9</i>. Here 4 and 5 are called operands and + is called operator. PHP language supports following type of operators.</p>
<p>Lets have a look on all operators one by one.</p>
<h2>Arithmetic Operators</h2>
<p>There are following arithmetic operators supported by PHP language −</p>
<p>Assume variable A holds 10 and variable B holds 20 then −</p>
<p><a href="/php/php_arithmatic_operators_examples.htm">Show Examples</a></p>
<h2>Comparison Operators</h2>
<p>There are following comparison operators supported by PHP language</p>
<p>Assume variable A holds 10 and variable B holds 20 then −</p>
<p><a href="/php/php_comparison_operators_examples.htm">Show Examples</a></p>
<h2>Logical Operators</h2>
<p>There are following logical operators supported by PHP language</p>
<p>Assume variable A holds 10 and variable B holds 20 then −</p>
<p><a href="/php/php_logical_operators_examples.htm">Show Examples</a></p>
<h2>Assignment Operators</h2>
<p>There are following assignment operators supported by PHP language −</p>
<p><a href="/php/php_assignment_operators_examples.htm">Show Examples</a></p>
<h2>Conditional Operator</h2>
<p>There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation. The conditional operator has this syntax −</p>
<p><a href="/php/php_conditional_operator_examples.htm">Show Examples</a></p>
<h2>Operators Categories</h2>
<p>All the operators we have discussed above can be categorised into following categories −</p>
<p>Unary prefix operators, which precede a single operand.</p>
<p>Binary operators, which take two operands and perform a variety of arithmetic and logical operations.</p>
<p>The conditional operator (a ternary operator), which takes three operands and evaluates either the second or third expression, depending on the evaluation of the first expression.</p>
<p>Assignment operators, which assign a value to a variable.</p>
<h2>Precedence of PHP Operators</h2>
<p>Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator −</p>
<p>For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedence than + so it first get multiplied with 3*2 and then adds into 7.</p>
<p>Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>PHP Decision Making IF ELSE and Switch Statements</title>
<h1>PHP - Decision Making</h1>
<p>The if, elseif ...else and switch statements are used to take decision based on the different condition.</p>
<p>You can use conditional statements in your code to make your decisions. PHP supports following three decision making statements −</p>
<p><b>if...else statement</b> − use this statement if you want to execute a set of code when a condition is true and another if the condition is not true</p>
<p><b>elseif statement</b> − is used with the if...else statement to execute a set of code if <b>one</b> of the several condition is true</p>
<p><b>switch statement</b> − is used if you want to select one of many blocks of code to be executed, use the Switch statement. The switch statement is used to avoid long blocks of if..elseif..else code.</p>
<h2>The If...Else Statement</h2>
<p>If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.</p>
<h3>Syntax</h3>
<h3>Example</h3>
<p>The following example will output "Have a nice weekend!" if the current day is Friday, Otherwise, it will output "Have a nice day!":</p>
<p>It will produce the following result −</p>
<h2>The ElseIf Statement</h2>
<p>If you want to execute some code if one of the several conditions are true use the elseif statement</p>
<h3>Syntax</h3>
<h3>Example</h3>
<p>The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise, it will output "Have a nice day!" −</p>
<p>It will produce the following result −</p>
<h2>The Switch Statement</h2>
<p>If you want to select one of many blocks of code to be executed, use the Switch statement.</p>
<p>The switch statement is used to avoid long blocks of if..elseif..else code.</p>
<h3>Syntax</h3>
<h3>Example</h3>
<p>The <i>switch</i> statement works in an unusual way. First it evaluates given expression then seeks a lable to match the resulting value. If a matching value is found then the code associated with the matching label will be executed or if none of the lable matches then statement will execute any specified default code.</p>
<p>It will produce the following result −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
if (<i>condition</i>)
<i>code to be executed if condition is true;</i>
else
<i>code to be executed if condition is false;</i>
</pre>
<h3>Example</h3>
<p>The following example will output "Have a nice weekend!" if the current day is Friday, Otherwise, it will output "Have a nice day!":</p>
<pre class="prettyprint notranslate tryit">
<html>
<body>
<?php
$d = date("D");
if ($d == "Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
</pre>
<p>It will produce the following result −</p>
<pre class="result notranslate">
Have a nice day!
</pre>
<h2>The ElseIf Statement</h2>
<p>If you want to execute some code if one of the several conditions are true use the elseif statement</p>
<h3>Syntax</h3>
<pre class="result notranslate">
if (<i>condition</i>)
<i>code to be executed if condition is true;</i>
elseif (<i>condition</i>)
<i>code to be executed if condition is true;</i>
else
<i>code to be executed if condition is false;</i>
</pre>
<h3>Example</h3>
<p>The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise, it will output "Have a nice day!" −</p>
<pre class="prettyprint notranslate tryit">
<html>
<body>
<?php
$d = date("D");
if ($d == "Fri")
echo "Have a nice weekend!";
elseif ($d == "Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
</pre>
<p>It will produce the following result −</p>
<pre class="result notranslate">
Have a nice day!
</pre>
<h2>The Switch Statement</h2>
<p>If you want to select one of many blocks of code to be executed, use the Switch statement.</p>
<p>The switch statement is used to avoid long blocks of if..elseif..else code.</p>
<h3>Syntax</h3>
<pre class="result notranslate">
switch (<i>expression</i>){
case <i>label1:</i>
<i>code to be executed if expression = label1;</i>
break;
case <i>label2:</i>
<i>code to be executed if expression = label2;</i>
break;
default:
<i>code to be executed