Category: PHP

Update Values of Entire Table using PHP

This code assumes you are connected to a MySQL database which has a table with Names and Emails. The idea is that it will output a table of every single value from that table,...

Truncate String by Words using PHP ?

Technique #1 <?php function trunc($phrase, $max_words) { $phrase_array = explode(‘ ‘,$phrase); if(count($phrase_array) > $max_words && $max_words > 0) $phrase = implode(‘ ‘,array_slice($phrase_array, 0, $max_words)).’…’; return $phrase; } ?> Technique #2 function limit_words($words, $limit, $append...

Server Side Image Resizer

The code uses PHP to resize an image (currently only jpeg). Using this method, the resized image is of much better quality than a browser-side resizing. The file size of the new downsized image...

PHP Array Contains

Check if value is in array and outputs Yes or No <?php $names = array( ‘Bob’, ‘Jim’, ‘Mark’ ); echo ‘In Array? ‘; if (in_array(‘foo’, $names)) echo ‘Yes’; else echo ‘No’; ?>

PHP Date Parameters

formatcharacter Description Example returned values Day — — d Day of the month, 2 digits with leading zeros 01 to 31 D A textual representation of a day, three letters Mon through Sun j Day of the month...

How to Find All Links on a Page using PHP

$html = file_get_contents(‘http://www.example.com’); $dom = new DOMDocument(); @$dom->loadHTML($html); // grab all the on the page $xpath = new DOMXPath($dom); $hrefs = $xpath->evaluate(“/html/body//a”); for ($i = 0; $i < $hrefs->length; $i++) { $href = $hrefs->item($i);...

How to Detect IE5 or IE6 using php code

PHP CODE: function getMSIE6() { $userAgent = strtolower($_SERVER[“HTTP_USER_AGENT”]); if (ereg(“msie 6”, $userAgent) || ereg(“msie 5”, $userAgent)) { return true; } return false; }

How to Detect AJAX Request using php

The HTTP_X_REQUESTED_WITH header is sent by all recent browsers that support AJAX requests. if ( !empty($_SERVER[‘HTTP_X_REQUESTED_WITH’]) && strtolower($_SERVER[‘HTTP_X_REQUESTED_WITH’]) == ‘xmlhttprequest’ ) { # Ex. check the query and serve requested data

Concatenate Array for Human Reading using PHP

function concatenate($elements, $delimiter = ‘, ‘, $finalDelimiter = ‘ and ‘) { $lastElement = array_pop($elements); return join($delimiter, $elements) . $finalDelimiter . $lastElement; } Usage $array = array(‘John’, ‘Mary’, ‘Ishmal’); echo concatenate($array); // outputs “John,...

How to Update Values of Entire Table using PHP?

This code assumes you are connected to a MySQL database which has a table with Names and Emails. The idea is that it will output a table of every single value from that table,...

How to get Zero Padded Numbers using PHP?

function getZeroPaddedNumber($value, $padding) { return str_pad($value, $padding, “0”, STR_PAD_LEFT); } Usage echo getZeroPaddedNumber(123, 4); // outputs “0123”

How to Change Graphics Based on Season using PHP?

<?php function current_season() { // Locate the icons $icons = array( “spring” => “images/spring.png”, “summer” => “images/summer.png”, “autumn” => “images/autumn.png”, “winter” => “images/winter.png” ); // What is today’s date – number $day = date(“z”);...

Backup Database code using php

Class to back up entire databases and email them out, or individual tables. <?php class Backup { /** * @var stores the options */ var $config; /** * @var stores the final sql dump...

PHP code for Automatic Copyright Year?

Current year only &copy; <?php echo date(“Y”) ?> With start year &copy; 2008-<?php echo date(“Y”) ?> Start date with error protection <?php function auto_copyright($year = ‘auto’){ ?> <?php if(intval($year) == ‘auto’){ $year = date(‘Y’);...

How to parse and process HTML/XML with PHP?

I prefer using one of the native XML extensions since they come bundled with PHP, are usually faster than all the 3rd party libs and give me all the control I need over the markup. DOM...

How to Compress CSS with PHP code?

Start your CSS files with this PHP (and name it style.php): <?php ob_start (“ob_gzhandler”); header(“Content-type: text/css; charset: UTF-8”); header(“Cache-Control: must-revalidate”); $offset = 60 * 60 ; $ExpStr = “Expires: ” . gmdate(“D, d M...

How can i solve this php errorr“Notice: Undefined variable”?

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globalsturned on. E_NOTICE level error...

How to prevent SQL injection in PHP?

Use prepared statements and parameterized queries. These are SQL statements that are sent to and parsed by the database server separately from any parameters. This way it is impossible for an attacker to inject malicious...

HTML Radio Buttons using PHP coding

A Radio Button is a way to restrict users to having only one choice. Examples are : Male/Female, Yes/No, or answers to surveys and quizzes. Here’s a simple from with just two radio buttons...

What is PHP?

PHP is probably the most popular scripting language on the web. It is used to enhance web pages. With PHP, you can do things like create username and password login pages, check details from...

How to create forms using PHP program

One of the main features in PHP is the ability to take user input and generate subsequent pages based on the input. In this page, we will introduce the mechanism by which data is...

How to Build a Calendar Table using PHP

<?php function build_calendar($month,$year,$dateArray) { // Create array containing abbreviations of days of week. $daysOfWeek = array(‘S’,’M’,’T’,’W’,’T’,’F’,’S’); // What is the first day of the month in question? $firstDayOfMonth = mktime(0,0,0,$month,1,$year); // How many days...

10 most advanced PHP Tips Revisited

Here, on the Smashing Editorial team, we always try to meet the expectations of our readers. We do our best to avoid misunderstandings, and we try to spread knowedge and present only the best...