PHP

From eBabel wiki
Jump to: navigation, search

PHP easter eggs

Try these on any site that runs PHP.

Display European accentuated characters

If the data has been stored with utf8 character set in mysql, to display the data read from the database, use:

utf8_decode(string or variable to display);

So for example:

echo utf8_decode("ñ"); // prints ñ

Source: http://stackoverflow.com/questions/4638579/wrong-symbol-exported-in-fpdf-n-as-a

Set the timezone to Amsterdam

<syntaxhighlight lang="php">

date_default_timezone_set('Europe/Amsterdam');

</syntaxhighlight>

Save a timestamp and ip address to mysql in Wordpress

<syntaxhighlight lang="php">

$wpdb->insert( 
	'tablename', 
	array( 
		'updated' => date('Y-m-d H:i:s',time()),
		'ipaddress' => $_SERVER['REMOTE_ADDR']
	)
);

</syntaxhighlight>

Format timestamp in PHP

<syntaxhighlight lang="php">

echo date('Y-m-d H:i:s',time());

</syntaxhighlight>

Read Json data POSTed to PHP script with jQuery AJAX call

<syntaxhighlight lang="php">

<?php
   // Read Json POSTed data from jQuery AJAX call.
   $data = json_decode(file_get_contents("php://input"));
 ?>

</syntaxhighlight>

Loop through a Json array of value

<syntaxhighlight lang="php">

<?php
   foreach ($data->blocks as $block)
   {
      echo $block;
   }
?>

</syntaxhighlight>

Read a single POST parameter sent via Json in jQuery/AJAX to PHP script

<syntaxhighlight lang="php">

<?php
   echo $data->listid;
?>

</syntaxhighlight>

Use the Wordpress framework to safely run SQL statement

The ? in the string leverages Wordpress to block SQL injection attacks.

<syntaxhighlight lang="php">

<?php
   $wpdb->query('SELECT * FROM table WHERE column = ?', array($id))
?>

</syntaxhighlight>

More on Wordpress and wpdb

Loop through a set of records read from mysql with Wordpress

<syntaxhighlight lang="php">

<?php
$result = $wpdb->get_results("SELECT * FROM wp_posts");
   foreach ( $result as $row )
   {
      echo $row->post_title;
   }
   
   echo count($result);
?>

</syntaxhighlight>

Create a guid

<syntaxhighlight lang="php">

<?php
   echo uniqid();
?>

</syntaxhighlight>

Get a PHP generated guid in jQuery ajax and create a cookie with jQuery plugin

<syntaxhighlight lang="javascript">

$.ajax({
 url: "/guid.php",
 type: "GET",
 dataType: "HTML",
 success: function(data) {
   $.cookie("guid", data, { expires: 365, path: "/" });
 },
 error: function(data) {
   // todo: implement error handling.
 },
 async: false
});

</syntaxhighlight>

For jQuery cookie, see jQuery plugin on GitHub.

Beginner tutorials

Object oriented programming with PHP - series of videos.

PHP 5 settings

By default, it's not possible to upload files larger than 2Mb via the browser and PHP. This setting can be changed.

First find the php.ini file. Use this command:

sudo find / -name php.ini

On my Ubuntu development environment, it's in:

/etc/php5/apache2/php.ini

Change the settings upload_max_filesize to 20M for example, instead of 2M, to allow for files as large as 20 Mb to be uploaded.