number such as 12.5 or 3.141592653589. Floating point numbers may be specified using either decimal or scientific notation. • <?php $temperature = 56.89; ?> Data Types in PHP
like "hello" or "abracadabra". String values may be enclosed in either double quotes ("") or single quotes(''). <?php $identity = 'James Bond'; $car = 'BMW'; // this would contain the string "James Bond drives a BMW" $sentence = "$identity drives a $car"; echo $sentence; ?> Data Types in PHP
also allows you to add strings with the string concatenation operator, represented by a period (.). Take a look: <?php // set up some string variables $a = 'the'; $b = 'games'; $c = 'begin'; $d = 'now'; // combine them using the concatenation operator // this returns 'the games begin now<br />' $statement = $a.' '.$b.' '.$c.' '.$d.'<br />'; print $statement; ?> Data Types in PHP
retrieve form data $age = $_POST['age']; // check entered value and branch if ($age >= 21) { echo 'Come on in, we have alcohol and music awaiting you!'; } if ($age < 21) { echo 'You\'re too young for this club, come back when you\'re a little older'; } ?> </body> </html> ageProcess.php
that "GET" should be used if and only if the form processing is idempotent. Idempotent means which typically means a pure query form. However, problems related to long URLs and non-ASCII character repertoires which can make it necessary to use "POST" even for idempotent processing
to the end of an existing array with the array_push() function: <?php // define an array $pasta = array('spaghetti', 'penne', 'macaroni'); // add an element to the end array_push($pasta, 'tagliatelle'); print_r($pasta); ?>
from the end of an array using the interestingly-named array_pop() function. <?php // define an array $pasta = array('spaghetti', 'penne', 'macaroni'); // remove an element from the end array_pop($pasta); print_r($pasta); ?>
into smaller components, based on a user-specified delimiter, and returns the pieces as elements as an array. <?php // define CSV string $str = 'red, blue, green, yellow'; // split into individual words $colors = explode(', ', $str); print_r($colors); ?>
file to write $file = '/directory/omelette.txt'; // open file $fh = fopen($file, 'w') or die('Could not open file!'); // write to file fwrite($fh, "Look, Ma, I wrote a file!\n") or die('Could not write to file'); // close file fclose($fh); ?>
session_start(); // this function checks the value of a session variable // and returns true or false function isAdmin() { if ($_SESSION['name'] == 'admin') { return true; } else { return false; } } // set a value for $_SESSION['name'] $_SESSION['name'] = "guessme"; // call a function which uses a session variable // returns false here echo isAdmin()."<br />"; // set a new value for $_SESSION['name'] $_SESSION['name'] = "admin"; // call a function which uses a session variable // returns true here echo isAdmin()."<br />"; ?>
if form has not been submitted // display form // if cookie already exists, pre-fill form field with cookie value ?> <html> <head></head> <body> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> Enter your email address: <input type="text" name="email" value="<?php echo $_COOKIE['email']; ?>"> <input type="submit" name="submit"> <?php // also calculate the time since the last submission if ($_COOKIE['lastsave']) { $days = round((time() - $_COOKIE['lastsave']) / 86400); echo "<br /> $days day(s) since last submission"; } ?> </form> </body> </html> <?php } else { // if form has been submitted // set cookies with form value and timestamp // both cookies expire after 30 days if (!empty($_POST['email'])) { setcookie("email", $_POST['email'], mktime()+(86400*30), "/"); setcookie("lastsave", time(), mktime()+(86400*30), "/"); echo "Your email address has been recorded."; } else { echo "ERROR: Please enter your email address!"; } } ?> </body> </html>
session_start(); ?> <html> <head></head> <body> <?php if (!isset($_SESSION['name']) && !isset($_POST['name'])) { // if no data, print the form ?> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <input type="text" name="name"> <input type="submit" name="submit" value="Enter your name"> </form> <?php } else if (!isset($_SESSION['name']) && isset($_POST['name'])) { // if a session does not exist but the form has been submitted // check to see if the form has all required values // create a new session if (!empty($_POST['name'])) { $_SESSION['name'] = $_POST['name']; $_SESSION['start'] = time(); echo "Welcome, " . $_POST['name'] . ". A new session has been activated for you. Click <a href=" . $_SERVER['PHP_SELF'] . ">here</a> to refresh the page."; } else { echo "ERROR: Please enter your name!"; } } else if (isset($_SESSION['name'])) { // if a previous session exists // calculate elapsed time since session start and now echo "Welcome back, " . $_SESSION['name'] . ". This session was activated " . round((time() - $_SESSION['start']) / 60) . " minute(s) ago. Click <a href=" . $_SERVER['PHP_SELF'] . ">here</a> to refresh the page."; } ?> </body> </html>
</head> <body> <?php // set database server access variables: $host = "localhost"; $user = "test"; $pass = "test"; $db = "testdb"; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create query $query = "SELECT * FROM symbols"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table cellpadding=10 border=1>"; while($row = mysql_fetch_row($result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No rows found!"; } // free result set memory mysql_free_result($result); // close connection mysql_close($connection); ?> </body> </html>