Post

PHP

PHP Basics!

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

Released in 1994. PHP: Personal HomePage Tools (Legacy name).

PHP 5: Powered by the new Zend engine 2, PHP 5 saw a fully rewritten object model that brought about dozens of new features, including OOP, and the PHP data objects or PDO. This created consistent methodologies for connecting to databases.

There were also a number of performance enhancements. PHP also showed maturation in another way. The development team grew from a few core developers to a worldwide group of dozens of developers. It was truly a team supported, honest to goodness software project. PHP 5.0 ushered in the modern era of PHP that we see today, and subsequent versions like 6, which introduced Unicode but was never formally released, 7, and now 8, continued the trend of standardizing features, and improving performance. Today, PHP powers everything from simple form processors, to popular content management systems, and even 3D games. This is largely thanks to the advancements made in PHP 5.0.

PHP works well with HTML, a popular scripting language. Most popular server side programming language. Builds dynamic websites PHP runs on a server. Requires a server to run.

PC based virtual servers include MAMP, AMPPS.

Without setting up a separate server, we can use an online compiler: https://www.w3schools.com/php/phptryit.asp?filename=tryphp_compiler

Basic PHP Syntax

PHP can be placed anywhere in the document.

  • PHP script starts with <?php and ends with ?>
  • // For comments
  • user designed functions, classes and keywords are NOT case sensitive.
  • variables are case sensitive.
  • link

Basic variable syntax

  • $variable_name = "my first variable"
  • 4 Basic variable types: boolean, integer, floating point, string.
  • To define a constant, use define. E.g.
  • <?php define("NAME", "VALUE"); ?>

We can directly inject PHP into html.

PHP Arrays: Store multiple values within a variable (Array).

  • $array_name = array("value1", "value2")
  • <?php echo $array_name[0]; ?>

Associative arrays follow the idea of dictionaries, where we can give a custom name to the key rather than using a number index.

1
2
3
4
5
6
7
8
9
10
11
12
<?php 
  $wine = array(
            name => "Merlot",

            type => "Red",

            agedDuration => 2

  );
?>

<?php echo $wine[name]; ?>

We can also do multi dimensional arrays.

1
2
3
4
5
6
7
8
9
10
11
12
$moustaches = array(

                array (
                  name=>"A",
                ),
                array (
                  name=>"B",
                ),
                array(
                  name=>"C",
                ),
);

IF ELSE ELSEIF Syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

  $a =20;
  $b = 50;

  if ($a < $b) {
    echo "Yep";
  } elseif ($a == $b) {
    echo "equal";
  } else {
    echo "No";
  }

?>

PHP Operators

  • =, +=, -= are assignment operators.
  • == is equal operator
  • === is identical operator (same type).
  • != or <> are not equal.

  • $a and $b / && are and operators.
  • $a || $b / or are or operators.

String Operators:

  • Concatenation operator: .: $b = $a . "Mi Amigos!";
  • Concatenating assignment operator .=: $a .= "Mi Amigos"

PHP Loops

  • While loop, for loop, for each loop, do while loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while (condition) {
  // do something
}

for ($a = 0; $a <= 20; $a++>) {
  echo "Number: $a <br>";
}


foreach ($array as $value) {

}

$moustaches = array("A", "B", "C");
foreach($moustaches as $moustache) {

}

do {

} while ( condition );

PHP Function

1
2
3
4
5
function functionName() {

}

functionName();
This post is licensed under CC BY 4.0 by the author.