HTTP Requests
$_GET
|
Access URL parameters. |
$_POST
|
Access POST request body content. |
$_SERVER['php_self']
|
Current php document url. |
Built-in Functions
include("x.php")
|
Includes the PHP file "x.php". |
echo "<content>"
|
Renders <content> on the page on load |
die( "message" )
|
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 information of $x |
empty($x)
|
Checks if $x is empty or false |
trim($x)
|
Removes leading and trailing whitespaces from $x |
PHP MS SQL DLL Functions
sqlsrv_connect
|
Connect to Microsoft SQL Server db. |
sqlsrv_query
|
Prepares and executes a query. |
sqlsrv_free_stmt
|
Frees resources for the statement. |
sqlsrv_close
|
Closes the SQL connection. |
sqlsrv_fetch_array
|
Returns a row as an array. |
sqlsrv_next_result
|
Move to the next result of the statement. |
sqlsrv_errors
|
Returns error and warning information. |
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>
|
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 literal"
|
A literal string |
// comment
|
Comments will not execute. |
"x"."y"
|
Concatenates "x" and "y" , resulting in "xy" |
Flow Control
if (x) {y} else if (a) {b} else {z}
|
Conditional 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);
?>
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets