Getting Started

Classes

class Student {
    public function \_\_construct($name) {
        $this->name = $name;
    }
}
$alex = new Student("Alex");

See: Classes

Constants

const MY\_CONST = "hello";
echo MY_CONST;   # => hello
# => MY\_CONST is: hello
echo 'MY\_CONST is: ' . MY_CONST; 

Comments

# This is a one line shell-style comment
// This is a one line c++ style comment
/\* This is a multi line comment
 yet another line of comment \*/

Functions

function add($num1, $num2 = 1) {
    return $num1 + $num2;
}
echo add(10);    # => 11
echo add(10, 5); # => 15

See: Functions

Include

#vars.php

<?php // begin with a PHP open tag.
$fruit = 'apple';
echo "I was imported";
return 'Anything you like.';
?>

#test.php

<?php
include 'vars.php';
echo $fruit . "\n";   # => apple
/\* Same as include,
cause an error if cannot be included\*/
require 'vars.php';
// Also works
include('vars.php');
require('vars.php');
// Include through HTTP
include 'http://x.com/file.php';
// Include and the return statement
$result = include 'vars.php';
echo $result;  # => Anything you like.
?>

Operators

$x = 1;
$y = 2;
$sum = $x + $y;
echo $sum;   # => 3

See: Operators

Arrays

$num = [1, 3, 5, 7, 9];
$num[5] = 11;
unset($num[2]);    // Delete variable
print\_r($num);     # => 1 3 7 9 11
echo count($num);  # => 5

See: Arrays

Strings

$url = "quickref.me";
echo "I'm learning PHP at $url";
// Concatenate strings
echo "I'm learning PHP at " . $url;
$hello = "Hello, ";
$hello .= "World!";
echo $hello;   # => Hello, World!

See: Strings

Variables

$boolean1 = true;
$boolean2 = True;
$int = 12;
$float = 3.1415926;
unset($float);  // Delete variable
$str1 = "How are you?";
$str2 = 'Fine, thanks';

See: Types

hello.php

<?php // begin with a PHP open tag.
echo "Hello World\n";
print("Hello quickref.me");
?>

PHP run command

$ php hello.php
Comments