PHP Fundementals

                                                                                                                                                                                       ele-running

πŸ”¨ Including PHP in a File

<?php

// place PHP code here

?>

πŸ”¨ Writing Comments

// β€” Denotes one line comment
# β€” Another way of one line comment
/*...*/ β€” Everything between /* and */ is not executed, also works across several lines

πŸ”¨ Outputting Data

echo "<h1>hamada</h1>";

❕ PHP echo and print Statements

echo and print are more or less the same. They are both used to output data to the screen.
echo                            vs             print
echo has no return value                       print has a return value of 1 so it can be used in expressions.
echo can take multiple parameters              print can take one argument.
echo is marginally faster than print.

πŸ’ͺ Variables and Constants

1️⃣ Defining Variables

<?php

$name = "hamada";

?>

important variable rule for naming variables :

Variables need to start with a letter or underscore (_) 
PHP variables are case sensitive
If your variable consists of more than one word either write it $my_variable or $myVariable

2️⃣ Types of Data

                                     PHP-Datatypes

  • Integers β€” Integers are non-decimal numbers between -2,147,483,648 and ,147,483,647 .
  • Floats β€” This is the name for numbers with a decimal point or in exponential form.
  • Strings β€” means text .
  • Boolean - True or False
  • Arrays β€” Arrays are variables that store several values.
  • Objects β€” Objects store both data and information on how to process it.
  • Resources β€” These are references to functions and resources outside of PHP.
  • NULL β€” A variable that is NULL doesn’t have any value.

3️⃣ Predefined Variables

                                                         SuperGlobales-Variables-in-PHP

  • $GLOBALS β€” Used to access global variables from anywhere inside a PHP script
  • $_SERVER β€” Contains information about the locations of headers, paths, and scripts
  • $_GET β€” Can collect data that was sent in the URL or submitted in an HTML form
  • $_POST β€” Used to gather data from an HTML form and to pass variables
  • $_REQUEST β€” Also collects data after submitting an HTML form

4️⃣ Constants

                                                                                     download

define(name, value, true/false)

⚠️ the third parameter whether its name should be case sensitive (the default is false).

default PHP constants:

  • LINE β€” Denotes the number of the current line in a file
  • FILE β€” Is the full path and filename of the file
  • DIR β€” The directory of the file

5️⃣ PHP Arrays

               Arrays-in-PHP-final

In PHP there are different types of arrays:

  • Indexed arrays – Arrays that have a numeric index
  • Associative arrays – Arrays where the keys are named
  • Multidimensional arrays – Arrays that contain one or more other arrays

indexed array :

<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);

for($x = 0; $x < $arrlength; $x++) {
  echo $cars[$x];
  echo "<br>";
}
?>

Associative arrays :

<?php
$human = array("hamada"=>"35", "ahmed"=>"37", "abdo"=>"43");

foreach($human as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>

Multidimensional arrays :

<?php
$cars = array (
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
);
    
for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$cars[$row][$col]."</li>";
  }
  echo "</ul>";
}
?>

PHP Functions :

  • A function is a block of statements that can be used repeatedly in a program.
  • A function will not execute automatically when a page loads.
  • A function will be executed by a call to the function.

User Defined Function in PHP:

<?php
function writeMsg() {
  echo "Hello world!";
}

writeMsg(); // call the function
?>

PHP Operators:

                                                85d5acbcbf874ffa23001f8591ff35e2

standard mathematic operators :

 + β€” Addition
 - β€” Subtraction
 * β€” Multiplication
 / β€” Division
 % β€” Modulo (the remainder of value divided by another)
 ** β€” Exponentiation

Assignment Operators :

 += ----> a += b is the same as a = a + b
 -= ----> a -= b is the same as a = a – b
 *= ----> a *= b is the same as a = a * b
 /= ----> a /= b is the same as a = a / b
 %= ----> a %= b is the same as a = a % b

Comparison Operators:

== β€” Equal
=== β€” Identical
!= β€” Not equal
<> β€” Not equal
!== β€” Not identical
< β€” Less than
> β€” Greater than
<= β€” Less than or equal to
>= β€” Greater than or equal to
<=> β€” Less than, equal to, or greater than

Logical Operators:

and β€” And
or β€” Or
xor β€” Exclusive or
! β€” Not
&& β€” And
|| β€” Or

Increment/Decrement Operators:

++$v β€” Increments a variable by one, then returns it
$v++ β€” Returns a variable, then increments it by one
--$v β€” Decrements the variable by one, returns it afterward
$v-- β€” Returns the variable then decrements it by one

PHP Loop:

                                                   different-types-of-Loops

while :

<?php
$x = 0;

while($x <= 100) {
  echo "The number is: $x <br>";
  $x+=10;
}
?>

do while :

<?php
$x = 6;

do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 5);
?>

for :

<?php
for ($x = 0; $x <= 100; $x+=10) {
  echo "The number is: $x <br>";
}
?>

foreach :

<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value <br>";
}
?>

Conditional Statements :

  • If Statement
if (condition) {
    // code to execute if condition is met
}
  • If…Else
if (condition) {
    // code to execute if condition is met
} else {
    // code to execute if condition is not met
}
  • Switch Statement
switch (n) {
    case x:
        code to execute if n=x;
        break;
    case y:
        code to execute if n=y;
        break;
    case z:
        code to execute if n=z;
        break;

    // add more cases as needed

    default:
        code to execute if n is neither of the above;
}