Skip to content

Undefined variables in PHP template

I use PHP as a template language in my view layer, Is it possible to make the following a little cleaner and more concise? Unfortunately, we can not even define a function for that: Answer Using @ operator is the shortest alternative. Since PHP 7 you can also use the ?? operator. Which does exactly what the @…

JSONP with Laravel

Browser sends JSONP requets to Laravel, Laravel returns the results to browser. In the brower console, I get the warning: I believe this is due to improper headers in the JSONP response? How should this warning be fixed? PHP Response::JSON($result) will return the usual JSON response I believe, not the JSONP …

Display special characters returned from mySQL in drop down select box

I am trying to display utf8 characters in a drop down box which is pulled from mySQL (utf8_general_ci) the characters are returning as diamonds with a ? in the middle on. I have tried htmlspecialchars and htmlentities. And it returns a blank string. Site also has the charset set to utf-8. Thank you very much.…

Global variable is not working as expected in PHP

I’m trying to make my own simple framework in PHP. It’s going OK but I’m running into a problem. I redirect everything back to my index.php, and from there I start loading classes and functions. I split up the url into segments, which works fine, until I want to use the segments in a class. …

Email validation using regular expression in PHP

I am pretty much new with regular expression. I am developing a project in PHP and i need to validate email address. After searching in this site and google i found the following regular expression says it should work best. But when I use it I get error says, What is wrong with this code? Answer Use this inst…

the difference between unset and = null

From a random php.net post: If you are doing $whatever = null; then you are rewriting variable’s data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time. Apparently this is the undisputed truth…

How do I count occurrence of duplicate items in array

I would like to count the occurrence of each duplicate item in an array and end up with an array of only unique/non duplicate items with their respective occurrences. Here is my code; BUT I don’t where am going wrong! Answer array_count_values, enjoy 🙂 Result:

date_parse_from_format to unix timestamp

I have a question regarding the date_parse_from_format() method: $parsed = date_parse_from_format(“Y d M H:i T”, ‘2012 28 Nov 21:00 CET’); Returns associative array with detailed info about given …