Category Archives: PHP

Sort a multidimensional array by one of its fields

<?php $products = array( array( name=>"A", price=>4.5  ),                    array( name=>"C", price=>5.5  ),                    array( name=>"D", price=>2.5  ),             … Continue reading

Posted in PHP | Leave a comment

New in PHP 5.4.0

Shortened array syntax <?php   $myArray = []; // instead of array(); ?>

Posted in PHP | 2 Comments

Check if a variable is a specific class

is_a — Checks if the object is of this class or has this class as one of its parents bool is_a ( object $object , string $class_name [, bool $allow_string = FALSE ] ) Example <?PHP class exampleClass{     … Continue reading

Posted in PHP | Leave a comment

string to time

strtotime — Parse about any English textual datetime description into a Unix timestamp int strtotime ( string $time [, int $now = time() ] ) Examples <?php echo strtotime("now"); echo strtotime("10 September 2000"); echo strtotime("+1 day"); echo strtotime("+1 week"); echo … Continue reading

Posted in PHP | Leave a comment

parse XML in browser

Let the browser parse XML: header ("content-type: text/xml");

Posted in PHP | Leave a comment

extract filename or file extension from path

<?php $path_parts = pathinfo(‘/www/htdocs/inc/lib.inc.php’);   echo $path_parts[‘dirname’], "\n"; echo $path_parts[‘basename’], "\n"; echo $path_parts[‘extension’], "\n"; echo $path_parts[‘filename’], "\n"; // since PHP 5.2.0 ?>

Posted in PHP | Leave a comment

How to Base64 encode your images

If you want to create a thumbnail through ajax with jquery and json. you can let php return an base64 encoded image. $img_src = "img/sample.jpg"; $imgbinary = file_get_contents($img_src); $img_str = base64_encode($imgbinary); echo ‘<img src="data:image/jpeg;base64,’.$img_str.‘" alt="Alt text" />’;

Posted in PHP | Leave a comment