Tag Archives: Procedural programming languages

JSON and Zend Framework? – Zend_Json

That’s really a good Zend Framework’s class that help you do the encode/decode job very easily. First of all it escapes everything for you and second it prints a correct/valid code. Note that sometimes if you have a trailing whitespace after the closing PHP tag – ?> that will result in an error.

Here’s some code:

public function jsonAction()
{
	$data = array(3,4,'test', 'my-name' => 3,4);
 
	echo Zend_Json::encode($data);
 
	$this->_helper->viewRenderer->setNoRender(true);
	$this->view->layout()->disableLayout();
}

and the result is:

{"0":3,"1":4,"2":"test","my-name":3,"3":4}

Note that all integers are printed without double quotes – which saves some space!

JavaScript Objects Coding Style

JavaScript vs. PHP

Continuing from my post about PHP arrays coding style and following the comments of that post, I’d like to write a bit about JavaScript objects’ coding style.

You perhaps know that the term object is quite undefined or under estimated in the JavaScript world, but I’d speak about the key/value pairs in JS commonly formatted like that:

var obj = { key : 'value' }

Here you can add more and more key/value pairs, but what’s different from the PHP associative arrays and what’s the same and should be cosidered.

The Same as PHP?

I wrote about the alignment in PHP and hashes. Than I showed how I align them:

$arr = array(
   'short'   => 'val',
   'longkey' => 'val'
);

In JavaScript you should use the same technique of alignment:

var obj = {
   'short'   : 'val',
   'longkey' : 'val'
};

Some Differences

Yes, there are more differences, which is normal. First of all you don’t have the => notation in JavaScript and a : is used. Second and most important you cannot add a trailing comma after the last key/value pair. Note that in PHP that’s fine!

// that will throw an error in MSIE
var obj = {
   'short'   : 'val',
   'longkey' : 'val',
};

while this is OK in PHP and it’s encouraged:

$arr = array(
   'short'   => 'val',
   'longkey' => 'val',
);

PHP Associative Arrays Coding Style

No PHP programmer writes code without arrays. Sound strange, but as many programmers there are, as many ways to format the array notation there are.

My advice is to rely on a defined standard. I personally use the Zend Framework’s coding style.

So how to format the code? The first way is on the same line:

$myArray = array('key1' => 'value1',
		 'key2' => 'value2');

And the other, perhaps more clear way is to place the associative pairs on a new line:

$myArray = array(
	'key1' => 'value1',
	'key2' => 'value2',
);

Note that in the second example there’s a comma after the second pair. This is correct PHP syntax and is strongly encouraged!

It’s up to you which way to take. However it mainly depends on the case, but please use standards.

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];
 
?>

HTML Tag Semantics. STRONG vs. B!

Do You Use LABEL?

Have you ever mentioned the existence of the LABEL tag? Have you ever used it? I guess the majority of us don’t. Let’s take a look of the following chunk of code:

<label>username:</label> <input type="text" name="username" />

It looks familiar to every web developer, but the most common usage in web forms is:

<div>username:</div>

or

<span>username:</span>

How Looks a LABEL Tag?

In fact if you use LABEL for the form instead of DIV or SPAN you’ll get the exactly same result as layout, beside that most common usage is a DIV or SPAN in place of the forgotten LABEL?! Why is that?

html tags. Label vs Span
Can you see the difference between LABEL and SPAN?

For me the usage of DIV has become critically enormous. It may sound strange that there is a debate about the usage of one or another HTML tag, but don’t you think every little text label in a web project is surrounded by a DIV. Actually that makes clear that if you’d like to be semantically correct, than you should use LABEL.

The funny thing is that almost every project is loading its labels in a PHP array or object or whatever and there they are called “Labels”, but when it comes to HTML they are put into a DIVs.

Why Semantics Matters?

In fact this is important because this is really good for web robots. If this is not important for the human eye it is for a machine “eye”. That’s why there are two tags for bold text.

STRONG vs. B

You can see that most of the browsers display both tags exactly the same by default.

Bold vs Strong HTML tags

Why’s that? Because STRONG is semantically more important than <B>.

So if you want to make a text simply looking bold, wrap it in <B>, but if you want to make it important both for the human eye and for the machines, than use STRONG.

Source: complete demo.