Posts

Smarty variable value in quotation mark

if you want to insert smarty variable value into string that is already in smarty "variable" (function call), just append it with `......` like shown in this example: {$obj->myMethod("This is my value: `$value`")} Usefull for owne link-preprocessor/rewriter

Organize your input checking and error reporting. Escape from too many inner if's

It's common to check some variables and report error / unsucceful operation if value is bad or to check result of operations before going forward. It can provide a bad code structure, where you can't find yourself in middle of a ton of bracets. Try to use return or exit instead, this will provide a cleaner, maintable code: function compute( $a , $b ) { if ( $a = 3 ) { // output error return; } // logic } As you can see, second solution is much more clear.

Organize your global functions to static methods - alternative to structural/procedural programming

If you are not into Object Oriented Programming, and include files and functions everywhere, you can get lost in your own function names. The good alternative is to group your functions into classes with static methods: class UserModule { public static function getUserId() { ... } public static function removeUser( $id ) { ... } } UserModule::removeUser( 11 );

How to merge (join) two arrayis in PHP together ?

You can merge all elements of two and more arrays into one array by array_merger func: $summaricArray = array_merge( $firstPartArray , $secondPartArray ); This results in all elements from both arrays in summaricArray

User not logged in PHP when requesting from flash etc. because of invalid session.

This is way to set a session ID. Remember to call session_id before session_start. This is helpful for common issue when request from flash uploader (uploadify etc.) causes errors, because requested PHP file doesn'y knows that user is logged (see flash request as new, non-logged user). if ( isset( $_POST['sessionid'] ) ) { session_id( $_POST['sessionid'] ); } session_start(); Remember that you must set sessionid on POST (or GET) request. This is useful when you connect to php page from other source than web browser (especially flash upload for example). You can pass your session ID to flash object 'flashvars' and when reqiesting upload or other page from SWF you can post/get session variable to access the same session normal web browser request has and for example be logged as current user.

How to split an array to parts by comma or special character (string to array) ?

Use explode function for this job: $names = 'John,Margaret,Thomas'; $namesArray = explode( ',' , $names ); echo $namesArray[0]; echo $namesArray[1]; echo $namesArray[2]; You can also use a character sequence to split string like a "splitplace"

How to access global variable in php ?

If you are in a function on class method scope, you can gain access to a global variable by using a global keyword. $x = 5; function myFunc() { global $x; echo $x; } Remeber that using globals usually isn't a good idea. Better idea would be divide your globals by categories/modules and store it in static variables of some classes.