Show Menu
Cheatography

PHP Quick Reference Cheat Sheet by

Quick Reference for useful and basic PHP

HTTP Requests

$_GET
Access URL parame­ters.
$_POST
Access POST request body content.
$_SER­VER­['p­hp_­self']
Current php document url.

Built-in Functions

inclu­de(­"­x.p­hp")
Includes the PHP file "­x.p­hp".
echo "­<co­nte­nt>­"
Renders <c­ont­ent­> on the page on load
die( "­mes­sag­e" )
Exits the script, with a message.
isset­($a)
Returns true if $a is not NULL
isset($a, $b)
Returns true if $a and $b is not NULL
print­_r($x)
Prints readable inform­ation of $x
empty­($x)
Checks if $x is empty or false
trim($x)
Removes leading and trailing whites­paces from $x

PHP MS SQL DLL Functions

sqlsr­v_c­onnect
Connect to Microsoft SQL Server db.
sqlsr­v_q­uery
Prepares and executes a query.
sqlsr­v_f­ree­_stmt
Frees resources for the statement.
sqlsr­v_c­lose
Closes the SQL connec­tion.
sqlsr­v_f­etc­h_a­rray
Returns a row as an array.
sqlsr­v_n­ext­_re­sult
Move to the next result of the statement.
sqlsr­v_e­rrors
Returns error and warning inform­ation.

Example Form Loading

<?php
    // Get the parameter: "Fruit" from the URL query string.
    // Eg. https://example.php/?Fruit=Apple

    $Fruit = $GET['Fruit'];
?>
// Render the variable $Fruit in the HTML
<div>
    <?php echo $Fruit; ?>
</div>
Apple

Example SQL Query Insert

<?php
    $Name = "John Doe";
    $Age = 25;

    $db_server = "<server_hostname>";
    $db_username = "<database_username>";
    $db_password = "<database_password>";
    $db_database = "<database_name>";
    $connectionInfo = array("Database"=>$db_database, "UID"=>$db_username, "PWD"=>$db_password);
    $conn = sqlsrv_connect($db_server, $connectionInfo);

    $tsql = "INSERT INTO dbo.Customers ( Name, Age ) VALUES";
    // Append more string literals to the variable $tsql to form the full query
    $tsql .= "('".$Name."', '".$Age."')";

    $stmt = sqlsrv_query($conn, $tsql);

    // Free statement and connection resources.
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
?>
 

Basic Syntax

$variable = value
Creates a variable, can be of any type.
"string litera­l"
A literal string
// comment
Comments will not execute.
"x"."­y­"
Concat­enates "x" and "y", resulting in "xy­"

Flow Control

if (x) {y} else if (a) {b} else {z}
Condit­ional code block, if x is true, run y. Otherwise, if a is true, run b. Otherwise, run z
do {x} while (y)
A do ... while loop. Executes x as long as y is true
while(x) {y}
A while loop. Executes y as long as x is true

Comparison Operators

!$x
Returns true if $x is false
$x == $y
Returns true if $x matches $y in value
$x === $y
Returns true if $x matches $y in value and type
$x != $y
Returns true if $x does not match $y in value
$x !== $y
Returns true if $x does not match $y in value or type
$x <> $y
Same as $x != $y
$x && $y
Returns true if both $x and $y is true
$x || $y
Returns true if either $x or $y is true
$x > $y
Returns true if $x is greater than $y
$x < $y
Returns true if $x is less than $y
$x >= $y
Returns true if $x is greater than or equal to $y
$x <= $y
Returns true if $x is less than or equal to $y

Example Form Submission

<?php
    // Check if the request is a HTTP POST request. If it is, $_POST will not be empty.
    if ($_POST) {
        $Fruit = $_POST['Fruit'];
        // Fruit will have the value from the input element with the property: name="fruit" upon submission.
    }
?>

<form method="post" action="<?=$_SERVER['php_self']?>">
    <input type="text" name="fruit" id="fruit_id" />
    <input type="submit" value="Submit" />
</form>

Example SQL Query Select

<?php
    $db_server = "<server_hostname>";
    $db_username = "<database_username>";
    $db_password = "<database_password>";
    $db_database = "<database_name>";
    $connectionInfo = array("Database"=>$db_database, "UID"=>$db_username, "PWD"=>$db_password);
    $conn = sqlsrv_connect($db_server, $connectionInfo);

    $tsql = "SELECT FirstName, LastName FROM dbo.Customers";
    $stmt = sqlsrv_query($conn, $tsql);

    if( $stmt === false) {
        die( print_r( sqlsrv_errors(), true) );
    }

    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
          echo $row['LastName'].", ".$row['FirstName']."<br />";
    }

    // Free statement and connection resources.
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
?>
Doe, John
Sue, Mary
           

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          PHP Cheat Sheet
          PHP Cheat Sheet