<?php

<?php

PHP Tags

<?php

// Your code goes here

?>


<?

// Short open tag 

?>


<?php

// Omit the closing tag in pure PHP file

<?php

$name;

$7days; // False! Can't start with a number

$_7days; // True

$another_name;

$anotherName;

Variables

<?php

Comments

// Single line C/C++ style comment

# Another single line Perl style comment

/*

 Multi line comment.
 Yet another line of comment

*/


//================================
// CATEGORY LARGE FONT
//================================

//--------------------------------
// Sub-Category Smaller Font
//--------------------------------

/*
 * This is a detailed explanation
 * of something that should require
 * several paragraphs of information.
 */

<?php

echo vs print

echo($variable);

print($variable); // returns TRUE


// Echo allows us print multiple parameters

echo $variable, 123, $another_var, 'Some string';



<?= 'I love borsch!'; ?>

// Equals

<?php echo 'I love borsch!'; ?>

<?php

Data Types

Scalar:

  • boolean
  • integer
  • float (aka double)
  • string

Compound:

  • array
  • object
  • NULL
  • resource

Special:

<?php

Boolean

$foo = TRUE;
$bar = FALSE;


// They're case-insensetive

$spam = TrUe;
$eggs = fAlSe;

<?php

Integer and Float

$integer = 123;

$float = 3.14;

$plus = 3. + .14;

$sub = $plus - 1;

$mul = 2 * $plus;

$div = $plus / 3;

<?php

String

$string = 'Personal Home Page';
$double_quotes = "PHP Hypertext Preprocessor";


echo 'PHP' . ' ' . 'WordPress';


echo $string . $double_quotes;


echo strlen($string);

<?php

Array

$arr = array(1, 2, 3, 4 ,5);

$another_arr = [1, 2, 3];

echo $arr[1];

echo count($arr);

echo sizeof($another_arr); // Alias of count

$assoc = array('key' => 'val', 'key2' => 'val2', 
               ... ,
               'keyN' => 'valN');

<?php

NULL

A variable is considered to be null if:

  • it has been assigned the constant NULL.

  • it has not been set to any value yet.

  • it has been unset().

$null = NULL;

echo is_null($undefined_yet);

$variable = 'defined';
echo is_null($variable); 
unset($variable);
echo is_null($variable);

$variable = NuLl; // Case-insensetive

<?php

Constants

define('DB_NAME', 'creative');

define('DB_USER', 'creative');

define('DB_PASSWORD', '4M6TzwmfUa5vq2wh');

define('DB_HOST', '127.0.0.1');


echo DB_NAME;


<?php

Predefined Variables

echo $_POST['name'];

echo $_GET['email'];

$_POST
$_GET

$_REQUEST

<?php

if ... else

<?php
if ($condition == true) {
    echo $val;
}

if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}
?>

<?php if ( have_posts() ) : ?>

<!-- HTML goes here... -->

<?php endif; ?>

<?php

Foreach

foreach ($arr as $val) {
    echo $val;
}


foreach ($assoc as $key => $val) {
    echo $key . ' = ' . $val;
}


<?php foreach ($arr as $val) : ?>

<!-- HTML goes here... -->

<?php endforeach; ?>

<?php

Debugging

var_dump($variable);

print_r($variable);

die;

exit('Status message');

exit(1);

<?php

Include vs Require

// Emits a warning if it can't find a file

include 'file_to_include.php';


// Emits a fatal error

require 'file_to_include.php';

<?php

Tasks

<?php

Made with Slides.com