A Brief Overview of PHP

PHP is an open-source, server-side scripting language that was designed to be used with HTML. It can be object-oriented, and here is an overview of some of its features:

Data Types

Arrays

  • To create a variable and assign it to an empty array, use the following format $newarr = array();
  • To create a variable and assign it to an array, use: $newarr = array(1,2,3,4,5,6);

Associative Arrays

  • Associative arrays are an object-indexed collection of objects, used when you need a key to retrieve data.

Constants

  • The value of constants doesn’t change.
  • Use quotes when defining the constant, define("CONST_VALUE", 10), once it’s been defined, don’t use the quotes when referencing it.
  • You can’t redefine constants.

Booleans

  • When PHP outputs ‘true’ into a string, it outputs 1. For ‘false,’ it outputs nothing.
  • To check if a variable is a boolean, use is_bool();
  • True and False can be written in uppercase or lowercase

Floats

  • round(number1, number2) takes a number and how many significant digits you want there to be
  • ceil(number) ceiling always rounds up
  • floor(number) floor always rounds down
  • is_float(number) to determine whether something is a float

Functions

  • strtolower() changes a string to lower-case
  • strtoupper() changes a string to upper-case
  • ucfirst() changes first letter in string to upper-case
  • ucwords() changes first letter of every word in the string to upper-case
  • strleng() finds length of the string
  • trim() eliminates white spaces
  • strstr() find a string within a string (pass two parameters). Find a string within a string
  • str_replace() takes three parameters – the string you’re looking for, what you want to replace it with, and the item we’re searching in

Integers

  • abs(0 - 100) absolute value
  • pow(3, 6) exponential (first number to the power of the second number)
  • sqrt(25) square root
  • fmod(17, 3) modulo
  • rand() random
  • rand(1,20) random (min, max)
  • is_int(number) to determine whether something is an integer

Null and Empty

  • Case insensitive
  • is_null(); will test if an item is null
  • An empty string, null, 0, 0.0, “0”, false, and an empty array, are all considered items that are empty

Strings

  • Strings can include letters, text, html.
  • Use double quotes or single quotes to denote strings
  • Can do variable replacement inside of a string (but only when you use double quotes):
  <?php
  $phrase = "Hello World";
  ?>

  <?php
    echo "$phrase printed here<br>";
  ?>
  • For in place substitution, use echo "{$phrase} Here<br>";

Debugging

  • Use <?php phpinfo(); ?> to access more information about the configurations
  • Missing semicolons are a common problem
  • To turn on error reporting, in the php.ini file, display_errors = On and error_reporting = E_ALL, in PHP code, ini_set('display_errors', 'On'); and error_reporting(E_ALL);
  • Turn off error reporting on live websites
  • Use echo to make sure you’re getting the right values for variables
  • print_r($array); to print readable array
  • gettype($variable); to get variable type
  • var_dump($variable); to get type and value
  • get_defined_vars(); array of defined variables
  • debug_backtrace(); show backtrace

Encoding

  • For GET requests to pass a reserved character, you must encode them with urlencode($string) – letters, numbers, underscores, and dashes will be unchanged, but reserved characters become % + 2-digit hexidecimal, and spaces become +
  • For rawurlencode, letters, numbers, and dashes are unchanged, reserved characters become % + 2-digit hexidecimal, and spaces become %20
  • Use rawurlencode for the path, before the “?” section, and uses urlencode on the query string
  • <, >, &, and ” are reserved characters in HTML
  • HTML can be encoded with htmlspecialchars() and htmlentities()

Printing

To print items to the screen, use echo

Variables

  • Start with a $ followed by a letter or underscore
  • Can contain letters, numbers, underscores, or dashes and can’t contain spaces
  • Case-sensitive

Leave a Reply