Converting Values in PHP

Convert Boolean Text To Boolean Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP.  FILTER_VALIDATE_BOOLEAN provides a solution though: $UploadsEnabled = "false"; $UploadsEnabled = filter_var($UploadsEnabled, FILTER_VALIDATE_BOOLEAN); Convert Bytes To UInt32 $handle = fopen("myfile.bin", "rb"); $Contents = fread($handle, 20); $FileByteArray = unpack("C*",$Contents); //<<<Warning the outputted array starts from [1] […]

Read More

Global Variables

Any variable used inside a function is by default limited to the local function scope. If you want to access a variable used outside of the function you need to declare it as global within the function: function my_function () { global $the_outside_variable; echo $the_outside_variable; //<<This now works! }        

Read More

Built In Superglobal Variables

Warning When Using Superglobal Variables Hackers often use these to try and inject code etc.  When accessing superglobal variables ensure you sanitise them.  E.g. $CameFromPage = htmlentities($_SERVER[‘HTTP_REFERER’]); //htmlentities() converts things like < > ” \ etc into HTML strings like &lt; so they become harmless. Superglobal Variables Always available in all scopes $GLOBALS References all […]

Read More

Constants

Constants define (“MY_ROOT_PATH”, “/usr/local/httpdocs/”); Then to use $PathToUse = MY_ROOT_PATH; PHP Magic Constants __LINE__ The current line number of the file. __FILE__ The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved […]

Read More

Variables

Variables General Variable names must start with a ‘$’ character (to allow the php parser to work as fast as possible by instantly know it is a variable). A variable name must start with a letter of the alphabet or and underscore ‘_’. Variable names may only contain: ‘a’ – ‘z’, ‘A’ – ‘Z’ and […]

Read More