Jump to content

[SOLVED] accessing the array value witout putting it in another variable


asmith

Recommended Posts

Hi,

 

I'm looking for this syntax.

I wanna explode this string, and I need like the 3rd value on the array :

 

<?php

$a = 'The text must be exploded';

$exploded = explode(' ',$a);

echo $exploded[2];
?>

 

I wanna know, is it possible I echo it without putting it in the $exploded?

I mean directly, something like :

 

echo  explode(' ',$a)[2];

not that i'm aware of...is there a specific reason? this doesn't really help, but it's another trick:

<?php
$a = 'The text must be exploded';
list(,$value) = explode(' ',$a);
echo $value; //prints 'text'
?>

No big reason for that.

I don't need that variable anywhere in the script, so I was wondering about not making it at all.

Personally I love scripts as short as possible.

 

Honestly clean code is nice, but easy to read code is better. I tend to keep my code where people can know what is going on if someone is coming in behind me or to help me. Memory is abundant, so you might as well use it, especially with a simple thing as a variable or an array of variables.

 

The list is good for what you want, but say it explodes 50 characters and you never know which one you want (IE it is not always the second one). The array would be much easier/better imo for that situation.

 

It all depends on your ultimate goal.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.