You can’t put two __construct functions with unique argument signatures in a PHP class. I’d like to do this: What is the best way to do this in PHP? Answer I’d probably do something like this: Then if i want a Student where i know the ID: Or if i have an array of the db row: Technically you&…
How to store NULL values in datetime fields in MySQL?
I have a “bill_date” field that I want to be blank (NULL) until it’s been billed, at which point the date will be entered. I see that MySQL does not like NULL values in datetime fields. Do any of you …
Explode string only once on first occurring substring
preg_match() gives one match. preg_match_all() returns all matches. preg_split() returns all splits. How can I split only on the first match? Example: This is what I want: Answer Simply set $limit to 2 for 2 parts of the array. Thanks to @BenJames for mentioning: I tested and it works fine. The limit argument…
Parse string containing range of values to min and max variables
I’m looking for a nice way to parse a string into two variables using PHP. The variables are called minage and maxage, and they should be parsed according to the examples below: Answer Try this:
What http status code is supposed to be used to tell the client the session has timed out?
In a webpage, it uses YUI connection manager/datasource to send AJAX requests to the server, if the session (which contains the info on whether the user has been authenticated) has already timed out, those ajax responses that can only be viewed by authenticated users should return an http status code, telling…
Accessing @attribute from SimpleXML
I am having a problem accessing the @attribute section of my SimpleXML object. When I var_dump the entire object, I get the correct output, and when I var_dump the rest of the object (the nested tags), I get the correct output, but when I follow the docs and var_dump $xml->OFFICE->{‘@attributes…
Is object-oriented PHP slow?
I used to use procedural-style PHP. Later, I used to create some classes. Later, I learned Zend Framework and started to program in OOP style. Now my programs are based on my own framework (with …
Ignore case sensitivity when comparing strings in PHP
I’m trying to compare words for equality, and the case [upper and lower] is irrelevant. However PHP does not seem to agree! Any ideas as to how to force PHP to ignore the case of words while comparing them? Is there a way to carry out the replace even if the case is different? Answer Use str_ireplace to…
Jquery .ajax method=”post” but $_POST empty
$.ajax({ method: “post” , url: “save.php” , data: “id=453&action=test” , beforeSend: function(){ } , complete: function(){ } , success: function(html){ …
How would I skew an image with GD Library?
I want to skew an image into a trapezoidal shape. The left and right edges need to be straight up and down; the top and left edges need to be angular. I have no idea what the best way to do this is. I’m using GD Library and PHP. Can anyone point me in the right direction? Thanks, Jason Answer