Unsure of this PHP Practice

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I am working on a site that uses a PHP auction script. I am looking through the code and see that they initialize their variables like this:

(array) $src_cats = null;
(string) $subcategories_content = null;

I know it's good practice to initialize your variables, but I don't know why they are type casting it the way they are. According to PHP's website, you use the type in parenthesis to force a variable type. I sometimes use it in place of mysql_real_escape_string() when I am putting integers in MySQL queries.

The problem is I think they are using it in the wrong way. On PHP's website, all of their type casting is on the right side of the equals sign, which makes sense because they are assigning the result to the left side of the equals sign. In this script, they are using it on the wrong side, which means that it casts the variable, and then does nothing with the result.

Why are they doing this?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I could understand using type casting to make it so if you check the variable type, even though it is null, it would pass as an array, however I would think it would be this:

$arc_cats = (array) null;

Either was, I don't think I would use it that way, a little to confusing.

-Greg

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

Greg K is absolutely correct: $src_cats = (array) somevalue;

http://us.php.net/language.types.type-juggling

anyway, doesn't

$src_cats = array();

create an empty array?

and

$subcategories_content = "";

creates an empty string?

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

That's what I thought, which is why I don't understand why it's being used this way.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Yes, but there are times when you do have a difference between a null value and an "empty" value

code at work had to figure out a "glitch", there was an enum field in sql, either an empty string or "checked"

well the items that were imported in when it began, didn't have that field, so they were all NULL. In SQL $field<>"checked" is NOT true if the field is actually NULL. took me a little bit to track that one down (edited the table to set the default value to be empty sting)

Also anohter issue brought up.. isset($arrayName['key'] and array_key_exists('key',$arrayName) will return different values if $arrayName['key'] is NULL (the isset will return FALSE)

Usually for most programming though, treating "" and NULL the same works well, but there are times where a difference is noticed and/or needed.

-Greg

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Greg K wrote:
Usually for most programming though, treating "" and NULL the same works well, but there are times where a difference is noticed and/or needed.

-Greg


Yes, and the difference is because, well, there is a difference. Where would you draw the line to differences and specific definitions of something?

"Well, I only need a rough estimate, and as I'm working with numbers in the millions 1 is the same as 2 in my circumstance."
Using "one or the other" because "most" circumstances allow it without a problem is bad coding imo. The differences should be understood and the correct one used in the correct circumstances.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I agree, just use it because most of the time it works is bad practice.

You need to know the difference, and use it accordingly to the circumstance. I don't think their is a definite place to draw the line, it is up the programmer to have to ability to determine that when coding.

For example, If I have a div on a page to contain someones phone number if there was a phone number entered in a form, and I don't want the div to show if there wasn't one, I'm not going to check every possible thing about the variable.

$strPhoneNum = preg_replace("/[^0-9]/","",$_POST["txtPhoneNum"]);

if ($strPhoneNum != '') {
  echo "<div id='PhoneNum'>" . format_phone($strPhoneNum,'US') . "</div>\n";
}

(Note, I've tried editing this to display right, it still seems to be wrapping lines and changing fonts in the middle???)

In this case, i only care if there is at least a char. yes I could have use if(strlen($strPhoneNum)>0) but I prefer to not use a function if not needed.

-Greg

They have: 53 posts

Joined: Aug 2008

$arc_cats = (array) null; try it.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.