TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-20-2008, 05:46 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Confused (array) or array()?

I really don't know how to sum this up but uhh, what's better or is it all the same? Such as:
PHP Code:
// Different method in making variables, arrays...
(array) $foo;
// Wouldn't $foo become an array just by that simply?
// Or this way:
 
$foo = array();
// I understand $foo gets assigned using the = operator doing that turns it into an array but wouldn't the other method just as good?

// Sorry if I am confusing you >.<

// Otherwise is (array) good for only Conditional Executing Statements like IF()? :S 








James
Orc is offline  
Reply With Quote
Old 01-20-2008, 06:02 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

They are both valid ways but you end up with slightly different results.

PHP Code:
<?php
(array) $foo;
$bar = array();

var_dump($foo);    // echo's "NULL"
var_dump($bar);    // echo's "array(0) ( )
So as you can see, typesetting the variable to an array using (array) doesn't actually create a new empty array where as array() will.

For the sake of standards, you should probably use array() when creating new arrays. Things like (array) (int) (long) etc are generally only used when converting a variable type to another:

PHP Code:
<?php

$foo 
16.123;
$bar = (int) $foo;

var_dump($bar);    // echo's int(16)
In this example, we have Long ($foo) which we then convert to an int by typecasting it using (int).

Alan.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following 2 Users Say Thank You to Alan @ CIT For This Useful Post:
ETbyrne (01-20-2008), Orc (01-20-2008)
Old 01-20-2008, 06:23 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Alan @ CIT View Post
They are both valid ways but you end up with slightly different results.

PHP Code:
<?php
(array) $foo;
$bar = array();

var_dump($foo);    // echo's "NULL"
var_dump($bar);    // echo's "array(0) ( )
So as you can see, typesetting the variable to an array using (array) doesn't actually create a new empty array where as array() will.

For the sake of standards, you should probably use array() when creating new arrays. Things like (array) (int) (long) etc are generally only used when converting a variable type to another:

PHP Code:
<?php

$foo 
16.123;
$bar = (int) $foo;

var_dump($bar);    // echo's int(16)
In this example, we have Long ($foo) which we then convert to an int by typecasting it using (int).

Alan.
Thanks! :D That gave me the exact answer I wanted! :]
Orc is offline  
Reply With Quote
Old 01-20-2008, 07:00 PM   #4 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Great answer Alan!
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 01-20-2008, 07:52 PM   #5 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Explicit type casting shouldn't be used to initalize variables, but to ensure you get the right data type when using it (that's what I believe, however).

PHP Code:
$a 3// implicit cast to integer
$a 'abcd'// implicit cast to string

(float) $a 'asdf'// why? not useful at all 
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
Orc (01-20-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:28 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design