CodePamoja
CodePamoja means "Code Together" in Swahili. We are a group of passionate developers in The Netherlands and Kenya working together in virtual teams, transferring the latest software development skills to each other.
<?php // PHP inside a HTML page ?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>This is a HTML page</h1>
<?php
// Print 'Hello World!' on the HTML page
echo "Hello World!";
?>
</body>
</html>
<?php
// This is a single-line comment
# This is a single-line comment
/*
This is a multiple-lines
comment block that uses
multiple lines
*/
// Inline comment
$total = 2 /* + 3 */ + 4;
?>
<?php
$count = 12;
$name = 'John Doe';
?>
<?php
// Single quotes
echo 'Hello world!';
// Double quotes
echo "Hello world!";
?>
<?php
$total = 23654;
?>
<?php
$total = 23.654;
?>
<?php
$outcome1 = true;
$outcome2 = false;
?>
<?php
// Create an Array
array();
// Example of an Array
$music = array("Rock","Hardstyle","R&B");
// Check the length of an Array
echo count($music); // Output is: 3
// Associative Array
$hobbies = array("John"=>"Travelling", "Jane"=>"Kickboxing", "Joe"=>"Gaming");
?>
<?php
// Declare a class of Object
class Team {
function Team() {
$this->developer = "John Doe";
}
}
// Create an Object
$dev = new Team();
// Show properties of Object
echo $dev->developer; // Output is: John Doe
?>
<?php
// Empty variable will have NULL as value
$total;
// Empty a variable and set to NULL
$total = 10;
$total = null;
?>
<?php
echo "Hello world!";
?>
<?php
print "Hello world!";
?>
<?php
// If statement
if (condition) {
// Code gets executed if condition is true
}
// If else statement
if (condition) {
// Code gets executed if condition is true
} else {
// Code gets executed if the other conditions are false
}
// If elseif else statement
if (condition) {
// Code gets executed if condition is true
} elseif (condition) {
// Code gets executed if condition is true
} else {
// Code gets executed if the other conditions are false
}
?>
<?php
switch ($block) {
case option1:
// Code gets executed if $block = option1
break;
case option2:
// Code gets executed if $block = option2
break;
case option3:
// Code gets executed if $block = option3
break;
default:
// Code gets executed if $block is different than the rest
}
?>
<?php
while (condition is true) {
// Code gets executed
}
?>
<?php
for (init counter; test counter; increment counter) {
// Code gets executed
}
?>
<?php
do {
// Code gets executed
} while (condition is true);
?>
<?php
foreach ($array as $value) {
// Code gets executed
}
?>
<?php
function functionName($argument1, $argument2) {
// Do something, for example:
echo "$argument1 loves $argument2";
}
// Call the function
functionName("John", "traveling");
functionName("Jane", "horse riding");
?>
<?php
// Will only produce a warning and continues running
// File is not required and application should continue when file is not found
include 'filename.php';
?>
<?php
// Will produce a fatal error and stops running the script
// Use require when the file is required by the application
require 'filename.php';
?>
<?php
// Reads a file and writes it to the output buffer
echo readfile();
// Similar to readfile() but gives you more options by using parameters
// Will create a file if it doesn't exist yet
echo fopen();
// Reads from an open file, using parameters to specify max number of bytes to read
echo fread();
// Is used to close an open file
echo fclose();
// Is used to read a single line from a file
echo fgets();
// checks if the "end-of-file" (EOF) has been reached, can be used to loop through data
echo feof();
// Reads a single character from a file
echo fgetc();
// Is used to write to a file by using parameters
echo fwrite();
?>
<?php
setcookie(name, value, expire, path, domain, secure, httponly);
?>
<?php
$cookie_name = "admin";
$cookie_value = "John Doe";
// Create cookie with name and value, expire after 30 days, available entire app
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example Cookies</title>
</head>
<body>
<?php
// Use $_COOKIE global to retrieve value of cookie "admin"
if(!isset($_COOKIE[$cookie_name])) {
echo "Fail!";
} else {
echo "Cookie '" . $cookie_name . "' is set!";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
<?php
// Start the session
session_start();
// Remove all session variables
session_unset();
// Destroy the session
session_destroy();
?>
<?php
// Start session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example Sessions</title>
</head>
<body>
<?php
// To change a session variable, just overwrite it
$_SESSION["fav_car"] = "Maserati";
// Displays information about a variable in a way that's readable by humans
print_r($_SESSION);
?>
</body>
</html>
<?php
// Used to list what the PHP filter extension offers
filter_list();
// Filters a single variable with a specified filter
filter_var();
// Checks if a variable of a specific input type exists
filter_has_var();
// Returns the filter ID of a specific filter name
filter_id();
// Gets an external variable and optionally filters it
filter_input();
?>
<?php
// Set e-mail address
$email = "j.doe@example.com";
// Remove all illegal characters from email using e.g a constant
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate e-mail e.g. using a constant
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid e-mail address");
} else {
echo("$email is not a valid e-mail address");
}
?>
Notice: Undefined variable: VARIABLE_NAME in PATH_TO_FILE
Warning: include(PATH_TO_FILE): failed to open stream: No such file or directory
Fatal error: Call to undefined function MY_FUNCTION()
<?php
// Report simple running errors
error_reporting(E_ALL);
// Make sure they're on screen
ini_set('display_errors', 1);
// HTML formatted errors
ini_set("html_errors", 1);
$error_levels = array("E_ALL", "E_NOTICE", "E_WARNING", "E_ERROR",
"E_STRICT", "E_DEPRECATED", "E_PARSE");
...
?>
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
// Include the file you want to test
include('file.php');
?>
php_flag display_startup_errors on
php_flag display_errors on
php_value error_reporting -1
php_flag html_errors on
// Show all error levels
error_reporting: E_ALL
// Print errors to the screen
display_errors: ON
// Shows errors at startup, only use this setting when debugging
display_errors_startup: ON
// Error logging
log_errors: ON
// Path to log file
error_log: /My/Path/file.log
<?php
function customError($errno, $errstr) {
echo "<b>Error:</b> [$errno] $errstr<br>";
echo "Ending Script";
die();
}
set_error_handler(“customError”);
?>
<?php
$test = 2;
if ($test > 1) {
trigger_error("Value must be 1 or below");
}
?>
<?php
ini_set("log_errors", 1);
ini_set("error_log", "php-error.txt");
?>
# log errors
php_value log_errors 1
# log file for errors
php_value error_log php-error.txt
log_errors = On
error_log = "/YOUR/PATH/htdocs/php-error.txt"
By CodePamoja
Basic introduction
CodePamoja means "Code Together" in Swahili. We are a group of passionate developers in The Netherlands and Kenya working together in virtual teams, transferring the latest software development skills to each other.