Categories
PHP

PHP isset() vs empty() vs is_null()

PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.

isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.

isset()

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.

empty()

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

is_null()

is_null — Finds whether a variable is NULL

In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.

The table below is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false).
Value of variable ($var) isset($var) empty($var) is_null($var)
“” (an empty string) bool(true) bool(true)
” ” (space) bool(true)
FALSE bool(true) bool(true)
TRUE bool(true)
array() (an empty array) bool(true) bool(true)
NULL bool(true) bool(true)
“0? (0 as a string) bool(true) bool(true)
0 (0 as an integer) bool(true) bool(true)
0.0 (0 as a float) bool(true) bool(true)
var $var; (a variable declared, but without a value) bool(true) bool(true)
NULL byte (“\ 0?) bool(true)
I have tested the above values in following environments:

  • Wampserver 2.1, Apache 2.2.21, PHP 5.3.8
  • Wampserver 2.1, Apache 2.2.17, PHP 5.3.5
  • Wampserver 2.1, Apache 2.2.17, PHP 5.2.11

You can use the code below to get the above table.

<?php
echo '<table border="1">';
echo '<tr><th>Value of variable ($var)</th><th>isset($var)</th><th>empty($var)</th><th>is_null($var)</th></tr>';
$var = '';
echo '<tr><td>"" (an empty string)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = ' ';
echo '<tr><td>" " (space)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = FALSE;
echo '<tr><td>FALSE</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = TRUE;
echo '<tr><td>TRUE</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = array();
echo '<tr><td>array() (an empty array)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = NULL;
echo '<tr><td>NULL</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = '0';
echo '<tr><td>"0" (0 as a string)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = 0;
echo '<tr><td>0 (0 as an integer)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = 0.0;
echo '<tr><td>0.0 (0 as a float)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

unset($var); // doing this just as a precaution, to make sure $var is actually not defined.
$var;
echo '<tr><td>var $var; (a variable declared, but without a value)</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

$var = '\0';
echo '<tr><td>NULL byte ("\ 0")</td><td>';
var_dump(isset($var));
echo '</td><td>';
var_dump(empty($var));
echo '</td><td>';
var_dump(is_null($var));
echo '</td></tr>';

echo '</table>';
?>
'Coz sharing is caring
Categories
PHP

PHP Errors or Death Trap

  • Using a Search Engine for Finding Faulty Sites

PHP Errors Link BuildingI was reading the article about “Broken Link Building” the other day when I realized that there might be a possible extension to the idea of helping webmasters with keeping theirs sites together. Since there is a lot of stuff that can go wrong with a website, I started probing possibilities. Here is what I came up with for PHP Errors.

Definitely, there might be other problems with an internet site that might be noticed by an ordinary user. And in order to take a well-structured and organized approach, I had to find sites with a certain clear and present problem, and be able to find these sites in bulk.

While thinking about this, I was doing my usual everyday routine when suddenly a php error popped out on the site I was browsing at the time. You are no doubt have encountered something like that lots of times.

PHP Error Example

I knew for sure that I had seen such type of error A LOT. Only all of those times I was in an absolutely different frame of mind and had no idea how I could use it to my benefit.

The site was related to my own (that’s why I was browsing it in the first place), and of pretty decent quality and value, so it made perfect sense for me to ask a responsible person for a link from it. Only all of us know that you do not simply contact a person and ask them to link to you right away. You wouldn’t, would you?

PHP is getting out of control?

From experience we all know that all our requests that generate little or no value to the requestee should better be based on relationships, even if they are established by one simple sentence which says “Hi man, it seems your PHP is getting out of control, you had better do something about it: <link to the page with a PHP problem>”.

OK, let me explain everything step by step.

What I was doing wrong

Try more subtle approach, show them that you sent the message only because you felt that it would be appropriate to let them know about the problem, not because you wanted to use it to your benefit.

By the way, I was thinking if there is any chance that you can link to my site. It seems your visitors might be interested in this sort of thing. Anyway, size it up for yourself and if you of the same opinion, kindly add the link and let me know

But let me explain everything step by step.

  • PHP Notification Explained

A lot of you probably have quite perfunctory understanding of PHP. So do I. The beauty of it is that you do not have to be a programmer to help webmasters with their PHP problems. I will try to explain in short what you should know in order to be ready to write a PHP error message.

Let’s assume that you already know that PHP is a server-side scripting language.

If something goes wrong and a php command/function can not be executed properly on a page loading in a browser, PHP engine throws up a notification on the page (like the one displayed on the picture above). Sometimes they are not displayed, though, if the webmaster has chosen the “not display notifications in browser” option in the PHP settings.

There are several major types of notifications, but all of them are uniform, which makes it possible for us to find them on Google. Let’s take a look at a couple of examples:

Warning: include(../inc_header.php) [function.include]: failed to open stream: No such file or directory in /home/actualad/public_html/hotel_soaltee_crowne_plaza.php on line 21

Notice: Uninitialized string offset: 0 in /var/www/odkryjpolske.pl/op3/functions/functions.php on line 1952

Fatal error: Call to undefined function tweetmeme() in /home/content/40/8396940/html/blog/wp-content/themes/magilas/single.php on line 54

Deprecated: Function ereg_replace() is deprecated in /var/www/virtual/sleepingparis.com/htdocs/admin/filemappa.php on line 18

Strict Standards: Non-static method DB::connect() should not be called statically in C:\wwwAmauta\php\dosmanosperu\connection\gateway.inc.php on line 28

I was able to spot five types of error notifications and then made an attempt to figure out a way to find them on Google so that the results were as relevant as possible. After taking a closer look I figured out that the part “php on line” was present in all the types of notifications. The only other part that seemingly remained the same were the words “Warning/Notice/Fatal Error/Deprecated/Strict Standards”

  • Seek…

So, in order to get results containing pages with PHP error notifications, you should form queries:

  1. “warning:” “[function.” “php on line”
  2. “notice:” “php on line”
  3. “fatal error:” “php on line”
  4. “deprecated:” “php on line”
  5. “strict standards:” “php on line”

But that might not be enough. Instead of getting search results with actual error notifications on faulty pages, you might stumble on a discussion of that error on some forum, or even the official PHP site.

The solution is as follows: add some relevant keywords to your query (defining the type of site you want to deal with). Let’s assume I have an online hotel reservation site and I want to get in touch with tour and hotel sites all over the world. I do the following:

  1. “warning:” “[function.” “php on line” intitle:tours
  2. “notice:” “php on line” intitle:tours
  3. “fatal error:” “php on line” intitle:tours
  4. “deprecated:” “php on line” intitle:tours
  5. “strict standards:” “php on line” intitle:tours

The result is more than satisfactory one. There are 1,380,000 results for the query “warning:” “[function.” “php on line” intitle:tours and even the last hundred results out of 1000 displayed on Google are at least 50% relevant to what I was searching for. I mean the pages displayed indeed have a php error notification on them and offer tour services.

But if you somehow feel that the results aren’t relevant enough, you can always expand your search query by adding additional keywords.

Expand your search for php errors

There is also a more thorough way to go. You may further brake down the types of PHP errors by the contents of a notification.

Let’s assume you have stumbled upon the warning notification which looks like:

Warning: include_once(language/mn.php) [function.include-once]: failed to open stream: No such file or directory in /hermes/waloraweb061/b490/pow.sndmn/htdocs/destination/index.php on line 34

It is the easiest one to solve since it clearly states that the file or directory are missing. So all you have to write to the webmaster is “check your files and their names carefully”

Let’s find the constant value of the message and delete all the information which changes (like the names of the files and folders, paths to them, etc.). And do not forget to attach your keywords!

That’s what we get after some tweaking.

“Warning:” “[function.include-once]: failed to open stream: No such file or directory in” “php on line” intitle:tours

The query produces 132 results, which is quite something to start with since you already have the solution to the problem in your pocket. Now all you have to do is send your message to the respective parties in the results and wait for them to reply!

  • … and destroy!

DestroyUsually, if you direct the attention of webmaster to the problem with their site, they should know what to do about it since they have probably earned being called “webmasters”. Still there are a considerable percentage of people who take care of their site to some extent without deep understanding of its mechanics. The site might have been created by a company for a client who does not know much in this sort of stuff. In this case, your ability to search for information on the Internet will enable you to warm up the hearts of mighty number of people who can add your link on their sites.

What I am talking about here is trying to get to the bottom of the problem before contacting the webmaster so that not only could you state the presence of it, but also could help with figuring things out. It’s nice if you are into PHP and can crack any related problem without referring to World Wide Web.

If, however, you are not that sort of person, you might want to read some specialized PHP forums:

PHP Freaks
SitePoint PHP Forum
PHP Help
Codewalkers PHP-Related Forum

If the search comes up with a page which contains a following notification:

Warning: Cannot modify header information – headers already sent by (output started at /home/zungahto/public_html/includes/joomla.php:836) in /home/zungahto/public_html/includes/joomla.php on line 697

you have enough information to be able to find a solution. Let’s try to perform the following search:

site:forums.phpfreaks.com “Warning: Cannot modify header information – headers already sent by”
site:sitepoint.com “Warning: Cannot modify header information – headers already sent by”

Google comes up with as many as 4,350 and 4,700 results for forums.phpfreaks.com and sitepoint.com respectively, which is one damn mighty pile to browse through. You might want to look through the top 10 and send the links to the discussions you deem appropriate in your message. Another way to go is just simply send the url of the forum so that the recipient could start a new thread for themselves to address their problem exclusively. Of course it’s not an exhaustive solution, but it will give the webmaster something to lean on. If you want to go hardcore though, you can plug in one of your company’s programmers to give advice to the recipient (only I think it should be one hell of a good site to go into such extremities for a link).

Common PHP Error messages

Finally, check out this list of common PHP error messages. If you want to proceed with the idea of searching for a specific sort of error, you might want to read this one and then continue on with the search.

Let’s assume I have chosen to proceed with the “use of undefined constant” error. I have some sort of solution already, poor as it is. (It’s in the document provided above). So all you need to do is search for this sort of messages:

“Notice:” “Use of undefined constant” “php on line” intitle:tours

And heeeere we go! 59 results.

  • Checking the Activities of the Website

Further on, the fact that an error notification has been around long enough for a search engine to index it means that the site is getting seriously out of hand and might have been completely abandoned by the crew, so before sending a message insure in some way that the site is still in business. I do it by performing these steps:

Checking the Activities

  1. Enter the query consisting of the advanced “site” operator along with the domain of the site in question. (site:phperrorsite.com)
  2. Collapse the list of the Google Search Tools under the “Show Search Tools” link on the left column of Google search result page.
  3. In the time selection section choose the past year option
  4. The options sort by relevance and sort by date will appear; choose the latter.
  5. Now pay attention to the date in the first search result. The more recent it is, the better. This, along with the number of the documents in the search results pertaining to the domain, gives you an approximate idea of what is going on with the site.

Remember, this is far from precise and I would be glad to hear of any other ideas about how one can discover whether a site has been abandoned or not.

  • Taking a Deeper Look into the Problems of a Website

You may also want to check whether there are any other php errors of one sort or another by performing a respective search query modified by the “site” search operator:

site:phperrorsite.com “warning:” “[function.” “php on line”

Problems of a WebsiteThe higher the number of the documents found, the higher the likelihood that the site owners/editors just do not give a rat’s ass about their website and won’t probably respond to your message of good intention.

Now you have ensured that the site is still kept a close watch on by finding out that there is only one document with a php error, which, in its turn, happens to be in some secluded corner of the site and might simply have been missed by the webmaster. There is no excuse for a php error notification right above the header on the main page of a site. I think twice before contacting sites of that sort. Like, how could they have missed that and even gave time for a search engine to index it!?

  • Composing a PHP errors notification email

Well, I guess I have fed you all I had on the subject. Now for the pivotal point – email composing. I guess it’s one of the most popular subjects among SEO blog writers, so I do not want to write something that has been written thousands of times before me. Try to check out the most recent article on the subject (at the time of this writing).

example email

Nevertheless, here is my way of doing business. I hope this example might be of use to someone.


Subject: <recipient’s name>, I have spotted a problem with your site.

HI, <recipient’s name>

I was browsing through your site phperrorsite.com out of professional curiosity. (I maintain an online hotel reservation site myself and) came upon a page that was obviously getting out of hand:

<the url of the page with a PHP error>

I understand how tiresome it might be to keep everything in line, so I have attached a document with possible solution to this <type of problem as specified in the error notification, “use of undefined variable”, etc> problem.

There are also quite a few discussion on the internet pertaining to this problem, check them out if you will:

<the link to a thread on PHP Freaks forum on the subject>

<the link to a thread on SitePoint PHP forum on the subject>

However, I would recommend you to start a new thread on one of the forums laying down all the details.

Please drop me a line at your convenience; I would like to know how things worked out for you.

Best of luck with your work!

<your name>


You could omit some of the stuff like specifying the urls of forum threads. To keep it simple, you could just mention the page with the error and ask them to get back to you. After the reply, you can move on to asking for a little favor. Hopefully, you will be granted one!


Hi again, <recipient’s name>

I am glad my message was of use to you. I guess we, webmasters, should back each other up when we can. We could learn a lot from each other.

By the way, I got this idea that maybe my hotel reservation services might be of use to some of your visitors. Hotel reservation and tour services go along quite well, don’t you think so?

Please check out my site kumar.swatantra.info and if you of the same opinion, kindly add a link to it where you think it would be most appropriate.

I hope I do not impose you. It’s just that it’s so hard to stay on surface these days, if you get my meaning.

Hope to have a word from you soon.

Rock On,

<your name>


Well, I guess that’s about it. It might not be the best message ever conceived, but it worked fine for me.

Thing to remember: I guess it’s not a good idea to ask for a link in very first message. Wait for a little while in order to establish some sort of connection with the webmaster. And when you feel that you get along quite well – make your pitch.

Remember, if you do not get link – you still get yourself a grateful person who might do you a favor of one sort or another later on. So, not time wasted.

I would be glad to hear any suggestions for improving my messages!

  • A Little Extra: Contacting sites with enabled directory browsing in Apache

I have been trying to make the point that you should be creative. Try different approaches based on something that has been done before. Helping webmasters to keep their site neat and clean is the core here.

So let’s take a look at another method you could use as a reason for contacting webmasters:

Directory listing

Looks familiar, doesn’t it? If you spend eight hours a day on the Internet (as most of us do), it’s something you have seen a lot of times. And you probably know that there is something wrong with the site when you stumble upon something like that. And “that” can be called “directory indexing/browsing enabled in Apache server”.

There is a whole movement of hackers excited about finding sites with enabled directory. The sites, webmaster of which were reckless enough not to disable this default feature of Apache server. Having this enabled constitutes a great security risk to your website. Since having enabled directory, there are a lot of ways in which the sensitive data could be manipulated. It can be even a lot easier for someone to breach your site if you are one of those guys who has a document with password for all of your accounts. Believe me, there is a lot more of those than you can think.

Besides, it simply looks untidy to find such page in Google results.

  • How to find sites with enabled directory browsing

There is a system and structure to everything. If you look at a couple of pages like the one on the picture, you will notice a pattern. You will realise that there are constant values which never change. Here is what I use for finding such pages:

intitle:index intitle:of port apache server

The query comes back with 100% relevant results. (OK, let’s say 95% relevant results just to be on the safe side)

But we do not want to contact random sites, do we? Almost all of us involved in a specific kind of business and we want to find sites relevant to it.

Assuming that my own site is dedicated to online hotel reservation, I modify the query as specified below:

intitle:tours intitle:index intitle:of port apache server

I come up with lots of tour sites around the world who have some security issues.

  • Here is what you want to do further on

KudosYou contact the webmaster (or anybody else involved with the site) and tell them to disable building of directory index by opening the apache config file “httpd.conf” and removing the word “indexes” found in it. After that you can gradually shift to the “quid pro quo” part of the conversation.

'Coz sharing is caring