PHP (Basics)

<?php // Header ob_start(); session_start(); // Config ini_set( "display_errors", true ); // Set to false on a live website error_reporting(E_ALL ^ E_NOTICE); // Report errors but not notices date_default_timezone_set( "Europe/Athens" ); // http://www.php.net/manual/en/timezones.php ini_set('default_charset', 'UTF-8'); // Word formatting strtolower($variable); // Lowercase strtoupper($variable); // Uppercase ucfirst($variable); // First letter of string is uppercase ucwords($variable); // First letter of each word is uppercase // Number formatting number_format($value, 2); // From: 123456.788 To: 123,456.79 // To be used for display purposes only, otherwise can mess up calculations // Date $now = date("Y-m-d H:i:s"); $today = date("Y-m-d"); $uts['day-before-yesterday'] = strtotime('-1 days'); // How many days ago // You can also use months and years $date = date('Y-m-d', $uts["day-before-yesterday"]); // Preserve brakes from data saved in text areas echo nl2br($variable); // Limit number of characters $newText = substr ($text ,0 ,14); // If more than 30 characters, cut on 27 and add ... at the end $text = (strlen($text) > 30) ? substr($text,0,27).'...' : $text; // Create constants define("NAME", "value"); // Checks if (defined('SOMETHING')) {} if (isset($_SESSION['SOME_SESSION'])) {} if (isset($_COOKIE['SOME_COOKIE'])) {} // Find the ID inserted after an INSERT query $last_id = mysql_insert_id(); // $_GET all values $info = http_build_query($_GET); // Redirect header("location:http://www.pagename.com"); // Increase memory limit ini_set("memory_limit","512M"); // Increase response time ini_set('max_execution_time', 120); // 2 minutes // Replace unknown values $pattern = '/width=".*?"/'; // What to replace $replacement = 'width="100"'; // What to replace with $newVar = preg_replace($pattern, $replacement, $var); // Repeat if necessary $pattern = '/height=".*?"/'; $replacement = 'height="100"'; $newVar = preg_replace($pattern, $replacement, $newVar); echo $newVar; // Case switch $q = $_POST['q']; switch ($q) { case "apple": echo "Result is apple"; break; case "pie": echo "Result is pie"; break; default: echo "Please choose an option"; } // While loop $i = 1; while ($i < 6) { echo "Counted to ".$i."...<br />"; $i ++; } // For loop for ($i = 1; $i <= 5; $i++ ) { echo "Counted to ".$i."...<br />"; } // Arrays $array = array( "Value 1", "Value 2", "Value 3", "Value 4" ); // Create array count($array); // Displays "4" // Implode / Explode $array = array(2, 7, 11, 29, 32); $test = implode(" | ", $array); // Returns 2 | 7 | 11 | 29 | 32 $test = explode(" | ", $test); // Returns array(2, 7, 11, 29, 32) in_array(7, $array); // Returns true or false $return = array_keys($array); // Returns only the keys $return = array_values($array); // Return only the values $return = array_key_exists("name", $array); // Check if a specific key exist array_push($array, "test"); // Adds on more element at the end array_pop($array); // Removes the last item array_shift($array); // Adds an element at the beginning // Using arrays in functions (example) $data = array ('name'=>'Jack Sparrow', 'department' => 'Captain', 'age'=> 35); insert_record($data); function insert_record($data) { $query = 'INSERT INTO companytable SET'; foreach($data as $field => $value) { $query .= $field.' = '.$value.', '; } } ?>

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.