PHP: The Array Element Doesn’t Exist – Suppress the Warnings

What’s the problem?

If you code PHP from couple of hours you are probably familiar with the following problem:

1
2
3
4
5
6
7
8
9
<?php
 
	error_reporting(E_ALL);
 
	$arr = array();
 
	echo $arr[3];
 
?>

The first and most common solution is just by adding the isset() statement and the code will look like that:

1
2
3
4
5
6
7
8
9
10
<?php
 
	error_reporting(E_ALL);
 
	$arr = array();
 
        if (isset($arr[3]))
	        echo $arr[3];
 
?>

However do you know there’s is another simple and elegant solution? Simply add the @ sign to suppress the element existence

1
2
3
4
5
6
7
8
9
<?php
 
	error_reporting(E_ALL);
 
	$arr = array();
 
	echo @$arr[3];
 
?>
Posted in micro tutorial, PHP | Tagged , , , , , , , , , | Leave a comment

Using rand() for a CAPTCHA!

What if you have to add a simple CAPTCHA for a web form asking the user to sum two numbers. Will you prefer to use rand() to generate those two numbers? Some may say this is too predictable!

Posted in PHP | Tagged , , , , , | 1 Comment

Burn Feeds in Zend Framework

Continuing with the Zend Framework’s series, here’s another one – how to burn your data into RSS feed within ZF.

Fairly simple. Collect all the data into an array and than …

$feed = Zend_Feed::importArray($feedArray, 'rss');
$feed->send();
Posted in micro tutorial, PHP, zend framework | Tagged , , , | 1 Comment

Debugging in Zend Framework

Perhaps “debugging” is a bit too strong. However when you’re dumping an array in PHP, you’d probably prefer the print_r or var_dump.

echo '<pre>';
print_r($array);
echo '</pre>';

But did you know that in Zend Framework there’s a built in Zend_Debug?

Zend_Debug::dump($array);

Does pretty much the same thing!

Posted in micro tutorial, PHP, zend framework | Tagged , , , , , , , | Leave a comment

Why mb_strlen() Doesn’t Return the Correct Lenght

It’s strange to notice that mb_strlen() doesn’t return always what you expect. The problem of course is in you, not in mb_strlen(). That’s because you probably doesn’t specify encoding.

echo mb_strlen($str, 'UTF-8');

Now that’s another case!

Posted in micro tutorial, PHP | Tagged , , , , , | Leave a comment