Jump to content

Using Implode & Explode


TheJoey

Recommended Posts

so i would make piece

$piece = $array['0']['1'];

 

 

Um...I'm not gonna test if that works or not, but I'm pretty sure it doesn't.

 

You could do:

$piece = $array['0'];
$piecetwo = $array['1'];


$combinepiece = "$pieceone$piecetwo";

;

 

Unless I'm not understanding you correctly.

Link to comment
Share on other sites

You'd want to use trim() as well to remove the white space (that or explode by ' | ' instead of '|'). There's also no point in surrounding the indexes of the array in single quotes because it's an integer.

 

Right, well originally I thought (blank) parsed while '' did not parse. And that still seems to be true, but as I tested speed, some reason parsing processed noticeably faster than non-parsed. Would you care to explain to me how this is?

Link to comment
Share on other sites

I'm not sure what you mean by '(blank)', but integers aren't meant to be used without quotes, so I'd guess that's why it's faster. As for what single quotes parse: Single quotes will parse variables as literals, and not their value. While double quotes will parse them.

 

eg

$var = 'Hello';
echo '$var'; // $var
echo "$var"; // Hello

 

Note that in that example normally you wouldn't even use "$var", because variables don't require quotes either. It's just an example.

Link to comment
Share on other sites

I'm not sure what you mean by '(blank)', but integers aren't meant to be used without quotes, so I'd guess that's why it's faster. As for what single quotes parse: Single quotes will parse variables as literals, and not their value. While double quotes will parse them.

 

eg

$var = 'Hello';
echo '$var'; // $var
echo "$var"; // Hello

 

Note that in that example normally you wouldn't even use "$var", because variables don't require quotes either. It's just an example.

 

But its improper not to have quotes. It comes under as an error. Also I thought PHP auto-assumes that if you are lacking quotes you mean single quotes. Perhaps I am mistaken there as well.

 

Also, why are integers faster without single quotes? Whats the reasoning behind this? I would think that not parsing the numbers would be quicker but clearly I am mistaken.

 

Link to comment
Share on other sites

It's improper to not have quotes when using strings, although sometimes it won't throw errors (depending on your reporting level).

 

eg (This is improper, but depending on your reporting level won't throw an error)

$arr = Array('first' => 'hello', 'second' => 'world');
echo $arr[first];

 

However not using quotes on integer indexes won't throw an error (It's actually preferred).

 

PHP Processes integers faster than strings, so that's why $arr[0] would be faster than $arr['0'].

Link to comment
Share on other sites

 

PHP Processes integers faster than strings, so that's why $arr[0] would be faster than $arr['0'].

 

Ah, excellent. That explains it. Thanks for the tid bit. Funny how you can have all these little things which will make your PHP script just a little bit faster at a time.

 

 

 

Gizmola, I already corrected that.  :P (Well, excluding the single quotes and no quotes which I just learned from AlexWD.)

Link to comment
Share on other sites

It's improper to not have quotes when using strings, although sometimes it won't throw errors (depending on your reporting level).

 

eg (This is improper, but depending on your reporting level won't throw an error)

$arr = Array('first' => 'hello', 'second' => 'world');
echo $arr[first];

 

However not using quotes on integer indexes won't throw an error (It's actually preferred).

 

PHP Processes integers faster than strings, so that's why $arr[0] would be faster than $arr['0'].

 

This really isn't how PHP works.  PHP has a hashed symbol table.  I doubt there's any noticeble difference, but technically there is probably a little bit more overhead in parsing:

 

$arr[0] compared to $arr['0']  -- that overhead being the need for PHP to check that zero is not a constant.  With that said, I would never bother to use $arr['0'] even though internally I know that all array indexes are strings.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.