Archiv der Kategorie ‘PHP‘

 
 

PHP: Uncomment PHP Code

php-logo

If you want to uncomment your PHP Code, have a look at my script:

https://gist.github.com/1375986

Usage:

# php uncomment /path/to/file.php

The script will read the content of the file, remove the comment, and write back to the file.
So the file will be CHANGED! You have been warned.

PHP: The big php variable type casting!

php-logoI wrote a small script that does every available cast in PHP on every datattype.
Check it out on GistHub:
https://gist.github.com/1341572

The output of the script should be the following:

$ php casting.php

--- Casting all in PHP available Types in every way ---

Info:
In PHP we have the following data types: boolean, int, float, double, array, object,
while float and double should be the same.
There are other types that can not be created by cast, like resources.
PHP is weak typed and allows to cast everything to everything.
I think this is something every (good) Software Programmer working with PHP should know.
Check out the results of the casts, especially to boolean, so you do not need
if(strlen($s) > 0 || count($a) > 0) ... anymore.

PHP-Version: 5.3.5-1ubuntu7.3

--- casting 'null' ------------
(boolean)null -> (boolean)false
(int)null -> (integer)0
(float)null -> (double)0
(double)null -> (double)0
(string)null -> (string)''
(array)null -> (array)array ()
(object)null -> (object)stdClass

--- casting '0' ------------
(boolean)0 -> (boolean)false
(int)0 -> (integer)0
(float)0 -> (double)0
(double)0 -> (double)0
(string)0 -> (string)'0'
(array)0 -> (array)array (0 => 0,)
(object)0 -> (object)stdClass

--- casting '1' ------------
(boolean)1 -> (boolean)true
(int)1 -> (integer)1
(float)1 -> (double)1
(double)1 -> (double)1
(string)1 -> (string)'1'
(array)1 -> (array)array (0 => 1,)
(object)1 -> (object)stdClass

--- casting '0.0' ------------
(boolean)0.0 -> (boolean)false
(int)0.0 -> (integer)0
(float)0.0 -> (double)0
(double)0.0 -> (double)0
(string)0.0 -> (string)'0'
(array)0.0 -> (array)array (0 => 0,)
(object)0.0 -> (object)stdClass

--- casting '0.1' ------------
(boolean)0.1 -> (boolean)true
(int)0.1 -> (integer)0
(float)0.1 -> (double)0.1
(double)0.1 -> (double)0.1
(string)0.1 -> (string)'0.1'
(array)0.1 -> (array)array (0 => 0.1,)
(object)0.1 -> (object)stdClass

--- casting '1.23' ------------
(boolean)1.23 -> (boolean)true
(int)1.23 -> (integer)1
(float)1.23 -> (double)1.23
(double)1.23 -> (double)1.23
(string)1.23 -> (string)'1.23'
(array)1.23 -> (array)array (0 => 1.23,)
(object)1.23 -> (object)stdClass

--- casting 'true' ------------
(boolean)true -> (boolean)true
(int)true -> (integer)1
(float)true -> (double)1
(double)true -> (double)1
(string)true -> (string)'1'
(array)true -> (array)array (0 => true,)
(object)true -> (object)stdClass

--- casting 'false' ------------
(boolean)false -> (boolean)false
(int)false -> (integer)0
(float)false -> (double)0
(double)false -> (double)0
(string)false -> (string)''
(array)false -> (array)array (0 => false,)
(object)false -> (object)stdClass

--- casting '""' ------------
(boolean)"" -> (boolean)false
(int)"" -> (integer)0
(float)"" -> (double)0
(double)"" -> (double)0
(string)"" -> (string)''
(array)"" -> (array)array (0 => '',)
(object)"" -> (object)stdClass

--- casting '"abc"' ------------
(boolean)"abc" -> (boolean)true
(int)"abc" -> (integer)0
(float)"abc" -> (double)0
(double)"abc" -> (double)0
(string)"abc" -> (string)'abc'
(array)"abc" -> (array)array (0 => 'abc',)
(object)"abc" -> (object)stdClass

--- casting 'array()' ------------
(boolean)array() -> (boolean)false
(int)array() -> (integer)0
(float)array() -> (double)0
(double)array() -> (double)0
(string)array() -> (string)'Array'
(array)array() -> (array)array ()
(object)array() -> (object)stdClass

--- casting 'array(1)' ------------
(boolean)array(1) -> (boolean)true
(int)array(1) -> (integer)1
(float)array(1) -> (double)1
(double)array(1) -> (double)1
(string)array(1) -> (string)'Array'
(array)array(1) -> (array)array (0 => 1,)
(object)array(1) -> (object)stdClass

--- casting 'array(1, 2)' ------------
(boolean)array(1, 2) -> (boolean)true
(int)array(1, 2) -> (integer)1
(float)array(1, 2) -> (double)1
(double)array(1, 2) -> (double)1
(string)array(1, 2) -> (string)'Array'
(array)array(1, 2) -> (array)array (0 => 1,1 => 2,)
(object)array(1, 2) -> (object)stdClass

--- casting 'fopen("php://stdout", "w")' ------------
(boolean)fopen("php://stdout", "w") -> (boolean)true
(int)fopen("php://stdout", "w") -> (integer)6
(float)fopen("php://stdout", "w") -> (double)7
(double)fopen("php://stdout", "w") -> (double)8
(string)fopen("php://stdout", "w") -> (string)'Resource id #9'
(array)fopen("php://stdout", "w") -> (array)array (0 => NULL,)
(object)fopen("php://stdout", "w") -> (object)stdClass

--- casting 'new A()' ------------
(boolean)new A() -> (boolean)true
PHP Notice: Object of class A could not be converted to int in casting.php(67) : eval()'d code on line 1
(int)new A() -> (integer)1
PHP Notice: Object of class A could not be converted to double in casting.php(67) : eval()'d code on line 1
(float)new A() -> (double)1
PHP Notice: Object of class A could not be converted to double in casting.php(67) : eval()'d code on line 1
(double)new A() -> (double)1
(string)new A() -> (string)'A'
(array)new A() -> (array)array ()
(object)new A() -> (object)A

--- PROBLEMS ------------
Some constructs in PHP need a parameter type to work correnctly,
like foreach() and many functions like implode() ....

foreach(array("works") as $a) {}
PHP Warning: Invalid argument supplied for foreach() in casting.php on line 78
PHP Warning: Invalid argument supplied for foreach() in casting.php on line 79
PHP Warning: Invalid argument supplied for foreach() in casting.php on line 80
implode('', array("works"));
PHP Warning: implode(): Invalid arguments passed in casting.php on line 84
PHP Warning: implode(): Invalid arguments passed in casting.php on line 85
PHP Warning: implode(): Invalid arguments passed in casting.php on line 86

PHP: Playing with stdClass

php-logoI wrote some code to check what PHP included stdClass is capeable of.
You can find the code at gisthub:
https://gist.github.com/1309031

stdClass is really just a wrapper around a array that can be casted to a array.
Good thing about it is, you can extend it with some failsave __get($key) function.

PHP: Universal NULL Object

When working with interfaces you sometimes need a object as parameter. Thats why i wrote this primity universal null object that does nothing at all and does not throw any errors:

<?php
/**
* Universal NULL object.
*
* Will throw no error messages so can be used as universal parameter.
* Does overwrite all available magic methods used in PHP5.3.
*
* @author Julius Beckmann
*/

class NullObject {
  public function __construct() {}
  public function __destruct() {}
  public function __set($name, $value) {}
  public function __get($name) { return null; }
  public function __isset($name) { return false; }
  public function __unset($name) {}
  public function __call($name, $args) {}
  public function __toString() { return ''; }
  public function __invoke() {}
  public function __clone() {}
  public static function __callStatic($name, $args) {}
}

GistHub:
https://gist.github.com/1101526

PHP: Doing some cool things with references and destructors

php-logo
Not everybody of you might know, that there are references in PHP too, like in many other languages. I wondered if there is something these references might be really good for and came up with the following concept.

Den ganzen Beitrag lesen...

Whiz: Simple cURL PHP wrapper class

I worked on a project where i needed to handle a lot with cURL request. At first i tried to use Zend_Http_Client, but it was too limited for my purpose. So i wrote my own cURL handler php class.

Den ganzen Beitrag lesen...

Facebook: Like Buttons uses wrong url

I use the digg-digg Wordpress Plugin on this blog and found a annoying error - the like button does not use the correct url. I found out why and how to correct it.

Den ganzen Beitrag lesen...

PHP: List of common pitfalls

I have seen a lot of PHP code, good and bad ones. Even some exploits for pretty common things that were just careless implemented.
In this post i want to share some of these pitfalls so you dont have to make them on your own.

Den ganzen Beitrag lesen...

PHP: Benchmark Inkrement und Dekrement

Wir haben alle in der Schule das kleine EinMalEins gelernt, doch selbst bei den simpelsten Anweisungen in Programmiersprachen gibt es noch Wissenlücken über Verwendung und Laufzeitunterschiede.
Im nachfolgenden Post wird mittels Benchmark ermittelt welche Art die schnellste ist eine Variable in PHP5 um 1 zu erhöhen (inkerment) oder zu verringern (decrement).

Den ganzen Beitrag lesen...

PHP: Everything you need to know about secure password hashing

On each site you need to register, your password has to be saved. This is typically done in a database. There are several ways to do this and some of them are good practice, other a very bad idea.

Den ganzen Beitrag lesen...