Posts Tagged php

Check an array for all null values

Here is a simple function to check an array to see if it contains all null values.

function allNulls($arr) {
    if(is_array($arr) && count(array_diff($arr, array(null))) == 0) { 
            return true;
    }
    
    return false;
}

echo (allNulls(array(null,null,null)) ? "true" : "false") . PHP_EOL;
echo (allNulls(array(null,1,null)) ? "true" : "false") . PHP_EOL;
echo (allNulls(array("test",null,null)) ? "true" : "false") . PHP_EOL;
echo (allNulls(array("",null,null)) ? "true" : "false") . PHP_EOL;
echo (allNulls(array(0,null,null)) ? "true" : "false") . PHP_EOL;
Share/Save

Tags: , , ,

Simple PHP Proxy

While developing apps that use external web services, a proxy often comes in handy in order to bypass the pesky XSS security settings found in most browsers. Here is a simple PHP proxy I’ve found quite helpful.



                      

Tags: ,

Using PHP to write PHP

I ran into a situation recently where I had a very large array object that was not going to change. It was the polygon coordinates for the world countries to be used in KML provided by Thematic Mapping.

I didn’t wan’t to pull this data out of MySQL every time I went to build a map, I wanted to use one giant static array instead. So to write the array back out in a re-usable fashion I developed the following code which can take any array, of any depth, associative or index based, and spit out corresponding PHP that you can copy and paste back into your script.

function phpPrintArr(array $arr) {
	echo "array(".PHP_EOL;

	$c = 1;
	$total = count($arr);

	foreach($arr as $i =>$v) {
		$iq = is_numeric($i) ? '' : '"';
		$vq = is_numeric($v) ? '' : '"';
		$e = $c < $total ? ',' : '';

		if(is_array($v)) {
			echo $iq.$i.$iq.'=>'.PHP_EOL;
			phpPrintArr($v);
			echo $e.PHP_EOL;
		} else {
			echo $iq.$i.$iq.'=>'.$vq.$v.$vq.$e.PHP_EOL;
		}
		$c++;
	}

	echo ")".PHP_EOL;
}

Oh, and for anyone who is interested in the array of country polygons this produced from the Thematic Mapping Engine data, here is the rather large array. Feel free to use it in your own KML project.

Tags: , , , ,

HipHop for PHP

Earlier this year Facebook developers caused quite a stir in the PHP community by releasing HipHop, the central piece in their high-performance arsenal.

Here is the video of the initial announcement along with some juicy technical details:

If you are interested in seeing how your PHP project fares when run through HipHop, check out the official HipHop project page.

With tools like HipHop, scripting languages like PHP are no longer subject to the charges of gross computational inefficiency. In short, it’s a great day to be a web developer.

Tags: , , , ,

SOAPjr Demo

A demo of SOAPjr using PHP/Symfony and ExtJS is now available at http://dev.communitybookshelf.org/

This demo showcases these custom components:

Questions/comments? We’d love to hear from you!

Tags: , , ,

Running PHP in Java

Many might consider even the thought of running PHP inside of a Java Virtual Machine to be anathema. Others will wonder why bother (apart from the novelty). However running PHP in Java has one crucal benefit: it future-proofs your code.

Quercus is a nifty utility that will allow you to run PHP code in clouds such as Google App Engine1. This means your Drupal and WordPress sites can now be distributed across a highly avaliable and scalable cloud infrustructure.

Now if we can only get an MVC framework like Kohana or Symfony to work on top of this system..

  1. Other great articles on running PHP in Google’s App Engine can be found here and here. IBM has also highlighted this utility. []

Tags: , , , , , ,

Magical PHP JSON Object Cleaner

I wrote this method the other day that takes a simple PHP object, inspects it’s properties and “prunes” empty ones. I wrote this method in order to compress JSON objects by removing null properties before sending them down the wire, a big problem when using base objects or models.

If you find this useful or a have a suggestion, feel free to let me know!

private function getStripped($obj) {
		$objVars = get_object_vars($obj);

		if(count($objVars) > 0) {
			foreach($objVars as $propName => $propVal) {
				if(gettype($propVal) == "object") {
					$cObj = $this->getStripped($propVal);
					if($cObj == null) {
						unset($obj->$propName);
					} else {
						$obj->$propName = $cObj;
					}
				} else {
					if(empty($propVal)) {
						unset($obj->$propName);
					}
				}
			}
		} else {
			return null;
		}
		return $obj;
	}

Tags: , , ,