<?php ?>
$myVar = 100;
define('GREETING', 'Hello Everyone');
$people = array('Kevin', 'Jeremy', 'Sara'); echo $people[0];
$cars = ['Honda', 'Toyota', 'Ford']; echo $cars[0]; $cars[] = 'BMW'; // add to the end of the array count($cars); // number of items in the array
$people = array('Brad' => 35); // Brad is a key, the value is 35
$people = array('Brad' => 35, 'Jose' => 32, 'William' => 37); echo $people['Brad']; $people['Jill']=42; // add to array
$cars = array( array('Honda', 20, 10), array('Toyota', 30, 20), array('Ford', 23, 12), ); echo $cars[1][2];
print_r($cars); // echo - show all array
var_dump($cars); // echo the datatype and the number of characters
echo "<pre>"; var_export($_SERVER['QUERY_STRING']);
Remove the first element from an array, and return the value of the removed element.
Push one or more elements onto the end of array
$names = array("Raya", "Nir", "Anat", "Shirly"); array_push($names, "Gill");
Split a string by a string. Return an array of strings.
$str = "hello,world"; $arr = explode(",", $str);
Join array elements with a string
$array = array('lastname', 'email', 'phone'); $space_separated = implode(" ", $array); echo $space_separated; // lastname email phone
Make a string lowercase
$str = strtolower($str);
Make a string uppercase
$str = strtoupper($str);
$output = substr('Hello', 1); // 1 is a start index --> ello
$output = substr('Hello', 1, 3); // 3 is an end index --> ell
$output = substr('Hello', -2); // start from the back --> lo
Returns the length of the string
$output = strlen('Hello World'); // 11
Finds the position of the FIRST occurence of a sub string
$output = strpos('Hello World', 'o'); // 4
Finds the position of the LAST occurence of a sub string
$output = strrpos('Hello World', 'o'); // 7
Returns 1 if string
$val = 'Hello'; $output = is_string($val);
Replace all occurence of a search string with a replacement
$text = 'Hello World'; $output = str_replace('World', 'Everyone', $text);
Capitalize every word
$output = ucwords('hello world');
Strips whitespace
$trimmed = trim($text);
<?php if($loggedIn): ?> <h1>Welcome </h1> <?php endif; ?>
<?php if($loggedIn): ?> <h1>Welcome User</h1> <?php else: ?> <h1>Welcome Guest</h1> <?php endif; ?>
<?php if($popup == 1):?> <div class="popup"> </div> <?php elseif($popup == 2):?> <div class="popup"> </div> <?php else:?> <div class="popup"> </div> <?php endif;?>
<div> <?php foreach($arr as $val): ?> <?php echo $val; ?> <?php endforeach; ?> </div>
<div> <?php for($i = 0; $i < 10; $i++): ?> <li><?php echo $i; ?></li> <?php endfor; ?> </div>
for($i = 0; $i < 10; $i++) { echo 'Number '.$i; echo '<br>'; }
$i = 0; while($i < 10) { echo $i; echo '<br>'; $i++; }
$i = 0; do { echo $i; echo '<br>'; $i++; } while($i < 10);
$people = array('Brad', 'Jose', 'William'); foreach($people as $person) { echo $person; echo '<br>'; }
$people = array('Brad' => 'brad@gmail.com', 'Jose' => 'jose@gmail.com', 'William' => 'william@gmail.com'); foreach($people as $key => $val) { echo $key.': '.$val; echo '<br>'; }
$server = [ 'Host Server Name' => $_SERVER['SERVER_NAME'], 'Document Root' => $_SERVER['DOCUMENT_ROOT'], 'Current Page' => $_SERVER['PHP_SELF'], 'Script Name' => $_SERVER['SCRIPT_NAME'], ]; <?php if($server): ?> <ul class="list-group"> <?php foreach($server as $key => $value): ?> <li class="list-group-item"> <strong><?php echo $key; ?>: </strong> <?php echo $value; ?> </li> <?php endforeach; ?> </ul> <?php endif; ?>
function sayHello($name = 'World') { echo "Hello $name<br>"; } sayHello('Raya');
function addNumbers($num1, $num2) { return $num1 + $num2; } echo addNumbers(5, 6);
function addTen(&$num) { $num += 10; }
$y = 1234; function x() { global $y; return $y; }
== === < > <= >= != !==
AND && OR || XOR only one is true, not both
switch($favColor) { case 'red': echo "
Your favorite color is red
"; break; default: echo "
Your favorite color is something else
"; break; }
<?php if($server): ?> <h2>Html is here </h2> <?php endif; ?>
<?php if (isset($_POST['name'])) { print_r($_POST); $name = htmlentities($_POST['name']); } ?> <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <div> <label>Name</label><br> <input type="text" name="name"> </div> <input type="submit" value="Submit"> </form>
... if($_SERVER['REQUEST_METHOD'] == 'GET') { $x = (int)htmlentities($_GET['x']); ...
<?php // Message Vars $msg = ''; $msgClass = ''; // Check for Submit if(filter_has_var(INPUT_POST, 'submit')) { // Get Form Data $name = htmlspecialchars($_POST['name']); $email = htmlspecialchars($_POST['email']); $message = htmlspecialchars($_POST['message']); // Check Required Fields if(!empty($name) && !empty($email) && !empty($message)) { // Check Email if(filter_var($email, FILTER_VALIDATE_EMAIL) !== false) { ... } else { // Failed $msg = 'Please fill a valid email.'; $msgClass = 'alert-danger'; } } else { // Failed $msg = 'Please fill in all fields.'; $msgClass = 'alert-danger'; } } ?>
<div class="container"> <?php if($msg !== ''): ?> <div class="alert <?php echo $msgClass; ?>"> <?php echo $msg; ?> </div> <?php endif; ?> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <div class="form-group"> <label>Name</label> <input type="text" name="name" class="form-control" value="<?php echo isset($_POST['name']) ? $name : ''; ?>"> </div> <div class="form-group"> <label>Email</label> <input type="text" name="email" class="form-control" value="<?php echo isset($_POST['email']) ? $email : ''; ?>"> </div> <div class="form-group"> <label>Message</label> <textarea type="text" name="message" class="form-control"><?php echo isset($_POST['message']) ? $message : ''; ?></textarea> </div> <button type="submit" name="submit" class="btn btn-primary btn-block">Submit</button> </form> </div>
<?php // Check for posted data // if (filter_has_var(INPUT_GET, 'data')) { // when method is get if (filter_has_var(INPUT_POST, 'data')) { echo 'Data found'; } else { echo 'Data not found'; } ?>
if (filter_has_var(INPUT_POST, 'data')) { $email = $_POST['data']; // Remove illegal chars $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Raya///<>()@////<>()test.com --> Raya@test.com echo $email.'
'; if(filter_var($email, FILTER_VALIDATE_EMAIL)) { echo 'Email is valid'; } else { echo 'Email is NOT valid'; } }
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="text" name="data"> <input type="text" name="data2"> <button type="submit">Submit</button> </form>
$filters = array( "data" => FILTER_VALIDATE_EMAIL, "data2" => array( "filter" => FILTER_VALIDATE_INT, "options" => array( "min_range" => 1, "max_range" => 100 ) ) ); print_r(filter_input_array(INPUT_POST, $filters));
$arr = array( "name" => "raya levinson", "age" => "35", "email" => "raya@gmail.com" ); $filters = array( "name" => array( "filter" => FILTER_CALLBACK, // apply function to this value "options" => "ucwords" ), "age" => array( "filter" => FILTER_VALIDATE_INT, "options" => array( "min_range" => 1, "max_range" => 120 ) ), "email" => FILTER_VALIDATE_EMAIL ); print_r(filter_var_array($arr, $filters));
FILTER_VALIDATE_BOOLEAN FILTER_VALIDATE_EMAIL FILTER_VALIDATE_FLOAT FILTER_VALIDATE_INT FILTER_VALIDATE_IP FILTER_VALIDATE_REGEXP FILTER_VALIDATE_URL
FILTER_SANITIZE_EMAIL FILTER_SANITIZE_ENCODED FILTER_SANITIZE_NUMBER_FLOAT FILTER_SANITIZE_NUMBER_INT FILTER_SANITIZE_SPECIAL_CHARS FILTER_SANITIZE_STRING FILTER_SANITIZE_URL
<?php include './inc/header.php'; ?> <h1>Home</h1> <?php include ('./inc/footer.php'); ?>
<?php require './inc/header.php'; ?> <h1>Contact</h1> <?php require './inc/footer.php'; ?>
<?php require_once './inc/header.php'; ?> <h1>About</h1> <?php require_once './inc/footer.php'; ?>
if(!empty($searchText)) { header('Location: https://www.google.co.il/search?q=' . $searchText); exit; }
echo date('d'); // Day i.e 30 echo date('m'); // Month echo date('Y'); // Year echo date('l'); // day of the week echo date('Y/m/d'); // 2020/08/30 echo date('d/m/Y'); // 30/08/2020 echo date('d-m-Y'); // 30-08-2020
echo date('h'); // Hour echo date('i'); // Minutes echo date('s'); // Seconds echo date('a'); // AM or PM echo date('h:i:sa'); // 11:53:16am echo date('h:i:s'); // 11:53:16
date_default_timezone_set("Asia/Jerusalem"); echo date('h:i:s');
Unix timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
$timestamp = mktime(10, 14, 54, 9, 10, 1981); //hours, munutes, seconds, month, day, year echo $timestamp; // 368957694 echo date('m/d/Y', $timestamp); echo date('m/d/Y h:i:s', $timestamp);
$timestamp2 = strtotime('7:00pm March 22 2016'); echo $timestamp2; // 1458669600 echo date('m/d/Y h:i:s', $timestamp2); // 03/22/2016 07:00:00
$timestamp3 = strtotime('tomorrow'); echo date('d/m/Y', $timestamp3);
$timestamp4 = strtotime('next Sund'); echo date('d/m/Y', $timestamp4);
// $timestamp5 = strtotime('+2 Month'); // $timestamp5 = strtotime('+2 Days'); $timestamp5 = strtotime('+2 Years'); echo date('d/m/Y h:i:s', $timestamp5);