PHP Coding Tips

Wednesday, January 30, 2013

Best way to secure e-mail in SMARTY template and CSS

See the best way to secure your e-mail from spambots. Just rearrange text flow to reverse and reverse an email with strrev() function:<span style="unicode-bidi:bidi-override; direction: rtl;"> {$sm_item.email|strrev} </span> Now you have your e-mail backwarded in page source, but displayed properly

Wednesday, November 14, 2012

A three-line guestbook in PHP

Below you can see a short script that causes to write somthing you enter a form to a file. Each entry is appended in new line.<form method=post><input name=a><input type=submit></form> <?php if($_POST['a'])file_put_contents('z.txt',$_POST['a']."\n",FILE_APPEND);

Monday, August 27, 2012

PHP File extension - How to get file extension ?

You can get a file extension in PHP using function pathinfo(). Here is sample usage:$extension = pathinfo( $fileName , PATHINFO_EXTENSION );

Monday, August 20, 2012

PHP Function default parameter value

You can set default parameter value for method or function by giving it's initial value at argument declaration, see example:function myFunc( $number = 8 ) { } If client will pass a number to that function like myFunc( 11 ), then $number value will be 11. But if user will call it without argument or parameter, the $number variable will have value of

How to remove white space on the left of a strin ?

You can use ltrim() function to remove white symbols at begining of a string, as example shows:$leftTrimmed = ltrim( $oldText );

PHP Store variable in a session

As you propably know, HTTP protocol is stateless, that means that each request run php parser and doesn't know about variables from previous page displayed.You can mantain variable values betweend numerous requests of the same user by storing this varaibles in session. To do that, you just have to call session_start() to start a session (a ID of session will be stored in browser cookies) and add a variable to $_SESSION array.See example below:session_start(); $_SESSION['myvalue'] = 5; Now, if you get $_SESSION['myvalue'] on other user request, you will get value of 5.

PHP Print state of a session

If you want to print all current variables stroed in session just use print_r function, as exaple shows:print_r( $_SESSION ); This will print all varaibles previously set in current session. Don't forget to call session_start() earlier to start a browser session

How to incrase a variable by given value ?

You can incrase a varaible value by number by += operator. If you want to incrase string by appending some data at the end, then use .= operator.See examples below:$number += 10; $string .= " and something";

How to access property of an object in PHP ?

You can access property of an object in php by opeartor ->.Just place it after a object variable, but remeber that accessed property must have valid access like private/protected/public.You can't access a private or protected object property from outside of a class or subclass.Item that you are accessing have to be public. You can access protected properties from subclasses and private properties if you are working with class wich have that property.Here is example how to access a public property in a object:class Sample { public $value; } $object = new Sample(); $object->value = 8; Remeber that good practice says to use getter and setter methods. If your project at some point will needa refactor that accessing some variable needs additional action then you can just change get or set method for that varaible.

Friday, August 17, 2012

Get last element of an array in PHP

To get last element of an array just use end() function. First idea can by get a index at count()-1 but remember that php arrays are associative arrays, so you can grab last element just by number.$lastElement = end($array); As simple as you see. It works even if your indexes are strings like 'name','surname' etc.