I want to extract the first word of a variable from a string. For example, take this input: The resultant output should be Test, which is the first word of the input. How can I do this? Answer You can use the explode function as follows: Another example:
Tag: php
Performance Improvement: Alternative for array_flip function
Is there any way I can avoid using array_flip to optimize performance. I am doing a select statement from database, preparing the query and executing it and storing data as an associative array in $…
Encrypting / Decrypting file with Mcrypt
Trying to write a couple of functions that will encrypt or decrypt a file and am using the class found here to try and accomplish this: http://www.itnewb.com/v/PHP-Encryption-Decryption-Using-the-…
PHP | define() vs. const
In PHP, you can declare constants in two ways: With define keyword Using const keyword What are the main differences between those two? When and why should you use one and when use the other? Answer As of PHP 5.3 there are two ways to define constants: Either using the const keyword or using the define() func…
CodeIgniter is generating multiple sessions in the database. Why?
I have a site which does a few ajax calls on page load. For some reason, CodeIgnitor is inserting 4 sessions (I’m assuming one for each ajax call) as you load the page. I’m storing the sessions in the database. I’m pretty sure there should only be one session per browser. Firefox seems to ge…
How to Debug Variables in Smarty like in PHP var_dump()
I have some variables inside a template and I don’t know where I assigned them. I need to know what is inside a particular variable; for instance, say I have a variable in smarty called member. I …
PHP: get number of decimal digits
Is there a straightforward way of determining the number of decimal places in a(n) integer/double value in PHP? (that is, without using explode) Answer
Finding the PHP File (at run time) where a Class was Defined
Is there any reflection/introspection/magic in PHP that will let you find the PHP file where a particular class (or function) was defined? In other words, I have the name of a PHP class, or an instantiated object. I want to pass this to something (function, Reflection class, etc.) that would return the file s…
How to encrypt data in javascript and decrypt in php?
Is there any javascript function that can encrypt data: For example i want to use encrypted data in my URL passed by ajax GET request, http://sample.com/mypage/TDjsavbuydksabjcbhgy where …
Is it possible to access outer local variable in PHP?
Is it possible to access outer local varialbe in a PHP sub-function? In below code, I want to access variable $l in inner function bar. Declaring $l as global $l in bar doesn’t work. Answer You could probably use a Closure, to do just that… Edit : took some time to remember the syntax, but here…