Skip to content

Latest commit

 

History

History
431 lines (348 loc) · 12.6 KB

php.md

File metadata and controls

431 lines (348 loc) · 12.6 KB

PHP

Variables

Naming Variables

Name & Example Type
$name = "Jim"; string
$number = 23; number
$Float = 55.67; float
$_included = true; boolean
$stuff_array = ["string", 44, false]; array
$nothing2 = null; null

Naming: Variables can only contain numbers, letters, and underscores, although they must begin with a letter or underscore. They are case sensitive.

Types: Variables can store the data types string, number, float, boolean, array, and null.



Modifying Variables

Set the value of the variable to its current value plus 5

$number += 5

Increment the variable value by 1

$number++;

Decrement the variable value by 1

$number--;



Casting Variables

The process of casting is forcing a variable to be another datatype.

$number_string = "3";

// Cast to integer
$number_int = (int) $number_string; // int(3)

// Cast to float
$number_float = (float) $number_string // float(3)

// Cast to string
$number_string = (string) $number_int; // string(1) "3"

// Cast to array
$number_string_array = (array) $number_string; // array(1) { [0] => string(1) "3" }

Useful internal constants

Constant Name Purpose Demo
PHP_EOL Outputs end of line character that is compatible with any system echo "End of Line" . PHP_EOL; // Displays: "End of Line" followed by a new-line character



Arrays

Creating Arrays

Declaring an array (two ways)
$car_makes = array("Ford", "VW", "Saab");
$car_models = ["Focus", "Jetta", "Miata"];
Indexed array
$car_models = ["Focus", "Jetta", "Miata"];

$car_models[1] // returns "Jetta"
Associate array
$car_models = [
  "model" => "Volkswagen", 
  "make" => "Passat", 
  "year" => 2013
];

$car_models["make"] // returns "Passat"
Multidimensional array
$car_parts = [
  "wheels" => ["hubcap", "rim", "tire", "lugnut"];
];

$car_parts["wheels"][3] // returns "lugnut"



Conditional Logic

if ($num_pizzas === 1) {
  echo "Give me more!";
} elseif ($num_pizzas === 3) {
  echo "I'm stuffed";
} else {
  echo "What does it matter? I'm unhealthy anyways.";
}

// If statements can also be written without curly braces if the statement within the condition is only one line
if ($num_pizzas === 1)
  echo "Give me more!";
elseif ($num_pizzas === 3)
  echo "I'm stuffed";
else
  echo "What does it matter? I'm unhealthy anyways.";

Switch statements can only check for equality, so if a more advanced condition needs to be evaluated, then an if statement is preferred.

switch ($num_pizzas) {
  case 1:
    echo "Give me more!";
    break;
  case 3:
    echo "I'm stuffed";
    break;
  default:
    echo "What does it matter? I'm unhealthy anyways.";
}

Ternary operators can be used to reduce code but can get confusing if too complex. Use sparingly.

echo ($num_pizzas === 1 ? "Give me more!") 
: ($num_pizzas === 3 ? "I'm stuffed")
: "What does it matter? I'm unhealthy anyways.";
}

A better implementation is for short if/then statements that are easy to read.

echo ($num_pizzas === 0) ? "Rage growing!" : "Mmm, love me some pie's";

Functions

A function is a collection of statements that execute together.

function welcome($name, $location) {
  // Return a value (in this case a string) from the function
  return "Welcome to {$location}, {$name}!";
}

echo welcome("David", "Downtown"); // Displays: Welcome to Downtown, David!

// If there are multiple values you want to pass into a function and they don't need to be accessed in any specific order, you can use the func_get_args() function which will return an array of all the values that have been passed as parameters to the function. You could, ofcourse, also just pass them as an array in the first place, but this is another way.
function add_numbers() {
  $numbers = func_get_args();

  $total = 0;
  foreach ($numbers as $number) {
    $total += $number;
  }
  
  return $total;
}

echo add_numbers(1, 5, 10, 20); // Displays: 36

Anyonymous functions provide a way to use a function (often as a parameter for another function) that will only be used once and serves no other purpose as a function to be called elsewhere.

// A function that runs a callback function
function speech ($message){
  echo "Let me clear my throat...Ok, I'm ready" . PHP_EOL;
  
  // Run callback
  echo $message();
}

// Call function using pre-defined function
function jfk() {
  return "Ich bin ein Berliner!";
};
speech("jfk");
// Returns
// -> Let me clear my throat...Ok, I'm ready
// -> Ich bin ein Berliner!
 
// Call function using anonymous function
speech(function(){
  return "Ich bin ein Berliner!";
});
// Returns
// -> Let me clear my throat...Ok, I'm ready
// -> Ich bin ein Berliner!

Useful internal functions

Function Name Action Demo
gettype() Returns the data type of the provided argument echo gettype(102) // Returns: "integer"

Loops

That are several control statements that can be used within loops:
break - break out of the loop entirely
continue - break out of the loop iteration and continue with the next iteration

While loop

A loop that will continue to run while a condition evaluates to true

$mph = 45;

while ($mph < 65) {
  echo "Get off the highway slow-poke! \n";
  $mph++;
}

A while loop is often used to loop through the results set returned from a database

// Provide example here

Do While loop

A do while loop will will always execute the code block at least once, before it checks the condition to determine if it should execute any longer.

$hunger_level = 100;
$chips = 5;

do {
  echo "Still hungry! Need chips now! \n";
  $hunger_level += $chips;
} while ($hunger_level > 30);

For loop

A for loop loops through a block of code a specified number of times. You specify the initial counter variable, the condition, and the incrementer all within the arguments.

The first section, the counter, is executed before the loop is executed.
The second statement, the condition, is evaluated every time the loop is executed.
The third statement is executed after the loop is executed.

for ($i = 0; $i <= 24; $i++) {
  echo "Hours finished for this day: {$i} \n";
}

Foreach loop

A foreach loop is used to loop through the items in an array. The number of iterations of the loop are, therefore, determined by the number of items in the array.

$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

foreach ($numbers as $number) {
  // If the number in the loop is odd, then break the iteration and continue with the next iteration
  if ($number % 2 !== 0) {
    continue;
  }
  
  // If the number is equal to 8, break out of the loop completely
  if ($number === 8) {
    break;
  }
  
  echo "The number {$number} made the cut! Phew!" . PHP_EOL;
}

// Foreach loops can also access key => value pairs
$employee_ages = [
  "John" => 14,
  "Nelson" => 25,
  "Eric" => 51,
];

// It is common to use $key and $value as the variables representing the key and value for the current iteration, but $name and $age is better this case as it provides context within the loop iteration
foreach ($employee_ages as $name => $age) {
  echo "{$name} is {$age} years old" . PHP_EOL;
}

A way to specify the data type accepted for the parameter of a function or method. If the correct data type is not provided when the function gets called, PHP will display an error.

// Provide example here (From functions chapter and later on)
// Show examples of type hinting for array, Closure, 
// Say hello to an array of people.
function sayHello(array $names) {
  foreach ($names as $name) {
    echo "Hello, {$name}!\n";
  }
}

sayHello(['Katie', 'Corissa', 'Lucy']); // Runs the function as expected
sayHello('Jeffrey'); // Displays error: Uncaught TypeError: Argument 1 passed to sayHello() must be of the type array, string given

Partials

There are several ways to include the code from another file within the current file, thereby eliminating the need to repeat code on several pages for repetitive functionality or markup.

Include

Include a file's contents on the current page. If it can't be found, it will continue to load the rest of the page without emitting an error.

  include 'includes/vendor/classes.php';

Require

Include a file's contents on the current page. If it can't be found, it will throw a fatal error.

  require 'includes/vendor/classes.php';
  
  // Use require_once if there's a possiblity it may be required multiple times
  require_once 'includes/vendor/classes.php';

Classes

A class is a collection of variables (called properties) and functions (called methods) that have a common organizational purpose. They are contained within an object, which can be accessed by creating an instance of (or instantiating) the class.

Within a class each property/method should have their scope, or visibility, declared. The scope defines the access available to the specified property or method.
public - Can be accessed from inside or outside a class
private - Can only be accessed from inside the class
protected - Can only be accessed from inside the class or its children
static - Add description here

// Define the Book class.
class Book
{
  // Declare properties.
  public $title;
  public $author;
  public $publisher;
  public $yearOfPublication;
  public $format = "paperback";
  private $book_identifier;
  
  // The constructor method is run anytime a class is instantiated. Any variables that need to have functions performed on them at instantiation must be be performed here. 
  function __construct($name)
    {
      $this->book_identifier = rand(5,999);
      $this->loading_message($name);
      $this->go_to_page(1);
    }
  
  // Declare a method.
  private function loading_message($name)
  {
    echo "Welcome {$name}" . PHP_EOL;
  }
  
  public function go_to_page(int $page_num)
  {
    return "Viewing page {$page_num}";
  }
  
  // Access values of the current instance from inside a class method by using the $this property
  public function summary()
  {
    echo 'Title: '      . $this->title        . PHP_EOL;
    echo 'Author: '     . $this->author       . PHP_EOL;
    echo 'Publisher: '  . $this->publisher    . PHP_EOL;
  }
}

// Create a new book instance.
$book = new Book("Jimmy");

// Set properties for this object.
$book->title                = 'Game of Thrones';
$book->author               = 'George R R Martin';
$book->publisher            = 'Voyager Books';
$book->yearOfPublication    = 1996;

// Echo out the property
echo $book->title . PHP_EOL;
// Displays
// -> Game of Thrones

// Execute the go_to_page() method.
echo $book->go_to_page(44) . PHP_EOL;
// Displays
// -> Viewing page 44

// Execute the summary() method.
$book->summary();
// Displays
// -> Title: Game of Thrones
// -> Author: George R R Martin
// -> Publisher: Voyager Books



Glossary

Term Definition
String Interpolation Embedding variable value in a string. It is best practice to wrap the variable in curly braces.
Example: echo "My name is {$name};"
Concatenation Joining two strings end to end.
Example: "Jim" + "Bob" // "JimBob"
Casting The process of transforming a variable from one data type to another



Links

Test Environments

http://sandbox.onlinephpfunctions.com/

Reference

PHP Pandas: http://daylerees.com/php-pandas/
PHP: The Right Way: http://www.phptherightway.com/
PHP Manual: Classes & Objects: http://php.net/manual/en/language.oop5.php

Syntax Documentation

OnlinePHPFunctions: http://onlinephpfunctions.com/
W3Resource: http://www.w3resource.com/php/function-reference/function-reference.php