Category Archives: PHP

PHP call_user_func vs just calling function

Always use the actual function name when you know it. call_user_func is for calling functions whose name you don’t know ahead of time but it is much less efficient since the program has to lookup the function at runtime. Also … Continue reading

Posted in PHP | Leave a comment

PHP ArrayAccess and ArrayObject

In very simple terms, ArrayAccess is an interface, which you can implement in your own objects; ArrayObject, on the other hand, is a class, which you can either use or extend. ArrayAccess is an interface built in to PHP which … Continue reading

Posted in PHP | Leave a comment

PHP Generators

Generators are functions that provide a simple way to loop through data without the need to build an array in memory. “yield” is the main command here. function getRange ($max = 10) { for ($i = 1; $i < $max; … Continue reading

Posted in PHP | Leave a comment

json_encode vs serialize

1) json_encode will produce a smaller string and is easy to read. 2) JSON is more portable. json is easily understandable by Javascript as well 3) JSON will have no memory of what the object’s original class was (they are … Continue reading

Posted in PHP | Leave a comment

Implement a Linked List in PHP

class Node { public $val; public $next; function __construct($val) { $this->val = $val; $this->next = $null; } } class LinkedList { public $head; function __construct() { $this->head = null; } function InsertNode($val) { $node = new Node($val); if ($this->head) { … Continue reading

Posted in PHP | Leave a comment

PHP Reading from command line

$handle = fopen (“php://stdin”,”r”); // if single input (having no spaces) fscanf($handle,”%d”,$n); // if trying to read 2 values separated by a space fscanf($handle,”%d %s”,$d,$s); // if trying to read a string having spaces $inp = trim(fgets($handle));

Posted in PHP | Leave a comment

Advanced Features of PHP

1) Late Static Binding: “Late binding” comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information. class A { public static function who() … Continue reading

Posted in PHP | Leave a comment

New Features in PHP 7

https://blog.feryn.eu/php-7-is-now-available-new-features-improvements/ 1) Speed 2) Type Declarations: Since PHP 5, you can use type hinting to specify the expected data type of an argument in a function declaration, but only in the declaration. When you call the function, PHP will check … Continue reading

Posted in PHP | Leave a comment

What is the difference between array_map, array_walk, array_filter, array_reduce in PHP?

array_walk() Applies the user-defined callback function to each element of the array array. Returns a boolean. May or May not Modify the original array. To modify original array callback function can accept an argument by reference. array_map() returns an array … Continue reading

Posted in PHP | Leave a comment

Traits vs Interfaces

Interfaces : An interface defines a set of methods that the implementing class must implement. Trait : When a trait is use’d the implementations of the methods come along too–which doesn’t happen in an Interface. http://stackoverflow.com/questions/9205083/traits-vs-interfaces

Posted in PHP | Leave a comment