PHP syntax

From Knowledge Kitchen
Jump to navigation Jump to search


This document contains a cheat-sheet of PHP syntax.

Variables, literals, and data types

A few notes:

  • Variable names in PHP always start with the dollar sign, $.
  • Statements always end in a semi-colon, ;
  • Variables are loosely-typed, and can store any data type


 1 <?php
 2 
 3 $a = 3;
 4 $b = 5.2;
 5 $c = true;
 6 $d = "String literals go in quotes";
 7 $e = '... or in single quotes';
 8 $f = array('this', 'is', 'an', 'array');
 9 $h = array(
10   'sauce': 'something to put on pasta',
11   'cream': 'has no place in coffee'
12 );
13 $g = $f[1];
14 $i = $h['cream'];
15 
16 ?>

Variable naming conventions

Variable names follow the usual naming conventions... pick your poison:

 1 <?php
 2 
 3 //underscore style
 4 $my_favorite_programming_book = "The Little Lisper";
 5 
 6 //lower camel case
 7 $theCarIDrive = "Reynolds Boughton RB 44";
 8 
 9 //upper camel case
10 $MyPreferredDrink = "kvass";
11 
12 //Hungarian notation
13 $iYearOfTheDog = 2018;
14 $fPi = 3.142;
15 $sArtificialCreamer = "is not cream";
16 $bPhpIsTheBest = false;
17 $aPrimes = array(1,3,5,7,11);
18 
19 ?>

Numeric addition vs. string concatenation

Numeric addition:

1 <?php
2 
3 $x = 4 + 12;
4 
5 ?>

String concatenation:

1 <?php
2 
3 $x = "this" . "that";
4 
5 ?>

Output

The most flexibility in PHP comes in the myriad ways in which it can output text.

Output using the print function

1 <?php 
2 
3 print("This will be output");
4 
5 ?>

Output using the echo command

1 <?php
2 
3 echo "This will also be output";
4 
5 ?>

Output using both the print and the echo commands

1 <?php
2 
3 echo "The print() function ";
4 print("and the echo statement ");
5 echo "do the same thing.\n";
6 print("It doesn't matter ");
7 echo "which one you use!";
8 
9 ?>

Output using no PHP at all

Don't use PHP if all you want to do is output text. Not using PHP at all is the preferred method for outputting HTML code

1 This will be output.  If you simply want to output something, you don't need PHP at all!

Output using both the print method and the "no PHP at all" method

1 This will be output, since it's not within the PHP block of code.
2 
3 <?php
4 print("And this will also be output, since it's an argument of the PHP print function call.");
5 ?>

Output using all the methods above

1 You <?php print("can"); ?> mix and <?php print("match"); ?> all of <?php echo "these"; ?> methods.

If statements

 1 <?php
 2 
 3 $x = 10;
 4 $y = 4;
 5 
 6 if ($x < $y) {
 7     print("kvass");
 8 }
 9 elseif ($x == $y) {
10     print("kefir");
11 }
12 else {
13     print("kombucha");
14 }
15 
16 ?>


Boolean logic operators

AND:

true && false

OR:

true || false

NOT:

!true

Equivalence:

5 != 2
"me" == "you"

Numeric comparisons:

5 < 10
4 <= 4
6 > 15
6 >= 32

For loops

1 <?php
2 
3 for ($i=0; $i<10; $i++) {
4     print($i);
5 }
6 
7 ?>

While loops

1 <?php
2 
3 $i = 0;
4 while ($i < 10) {
5     print($i);
6     $i++;
7 }
8 
9 ?>

Functions

Basic functions definition and function call:

1 <?php
2 
3 function do_something_fantastic() {
4     print("Something fantastic!");
5 }
6 
7 do_something_fantastic();
8 
9 ?>

Arguments:

1 <?php
2 
3 function do_something($very_special) {
4     print("Something " . $very_special);
5 }
6 
7 do_something("fantastic");
8 
9 ?>

Return values:

 1 <?php
 2 
 3 function get_something_fantastic() {
 4   return "Something fantastic";
 5 }
 6 
 7 $something = get_something_fantastic();
 8 print($something);
 9 
10 ?>

Arguments and return values:

 1 <?php
 2 
 3 function get_something($very_special) {
 4   return "Something " . $very_special;
 5 }
 6 
 7 $something = get_something("fantastic");
 8 print($something);
 9 
10 ?>



What links here