Trying to wrap my head around some code.

Hello all…

I have been slowly trying to learn PHP, and one of the things I am trying to teach myself is that although there seems to be many ways to do a certain thing, there is only one way to do that certain thing logically. Anyhow I have this piece of code that I am trying to understand, and I am hoping I can get someone to look at my notes relating to that code to see if I am understanding it correctly. So here is the code…

[php]

<? $return_string = array ( 0 => array ( 'type' => 'tag', 'code' => 'font', 'data' => '"times new roman"', 'closing' => FALSE ) ); $close_no_open = array ( 'i' => array ( 35, 28, 7 ), 'o' => array ( 17, 12, 4 ) ); $code_value = 'b'; ( isset ( $close_no_open[$code_value] ) === TRUE ? array_unshift ( $close_no_open[$code_value], ( end ( $return_string ) === FALSE ? 0 : ( key ( $return_string ) + 1 ) ) ) : $close_no_open[$code_value][] = ( end ( $return_string ) === FALSE ? 0 : ( key ( $return_string ) + 1 ) ) ); ?>

[/php]

So if I am understanding this correctly, this code could be rewritten like so…

[php]
// rewrite this to make it easier to understand
( isset ( $close_no_open[$code_value] ) === TRUE ? array_unshift ( $close_no_open[$code_value], ( end ( $return_string ) === FALSE ? 0 : ( key ( $return_string ) + 1 ) ) ) : $close_no_open[$code_value][] = ( end ( $return_string ) === FALSE ? 0 : ( key ( $return_string ) + 1 ) ) );

// rewritten…
if ( isset ( $close_no_open[$code_value] ) === TRUE )
{
if ( end ( $return_string ) === FALSE )
{
$next_key = 0;
}
else
{
$next_key = ( key ( $return_string ) + 1 );
}

array_unshift ( $close_no_open[$code_value], $next_key );

}
else
{
if ( end ( $return_string ) === FALSE )
{
$next_key = 0;
}
else
{
$next_key = ( key ( $return_string ) + 1 );
}

$close_no_open[$code_value][] = $next_key;

}
[/php]

So, this is what I am thinking these parts of the code mean…

This if(isset())

[php]if ( isset ( $close_no_open[$code_value] ) === TRUE )[/php]

Is used to check if the array $close_no_open[‘b’] exists, and if it does use array_unshift () to prepend the $next_key value to the array $close_no_open[‘b’]. but if it doesn’t exists…

[php]}
else
{[/php]

create the new array $close_no_open[$code_value][] and assign it the $next_key value.

So here is what I think each code block is doing…

[php]if ( isset ( $close_no_open[$code_value] ) === TRUE )[/php]

This isset() ? is used because array_unshift() would throw a warning if the array $close_no_open[‘b’] didn’t exist or wasn’t set, and the }else{ is used to create the unset array $close_no_open[‘b’] and assign it the $next_key value…

Then, this part of the code…
[php] if ( end ( $return_string ) === FALSE )
{
$next_key = 0;
}
else
{
$next_key = ( key ( $return_string ) + 1 );
}[/php]

Seems to check if the array $return_string has any any key => value pairs, and if it doesn’t, assign $close_no_open[‘b’][] the value of 0, but if the array $return_string did have one or more key => value pairs, then assign last $return_string(s) key value + 1 to the $next_key value. And this if()…

[php]if ( end ( $return_string ) === FALSE )[/php]

Is used to check if the array $return_string has any key => value pairs. So this if() is used to make sure they are assigning the correct key value to the array $close_no_open[‘b’][] that seems to be referencing the $return_string array.

Well hopefully I am understanding this code correctly, but I do have one other question…

Why would someone write… this!
[php]( isset ( $close_no_open[$code_value] ) === TRUE ? array_unshift ( $close_no_open[$code_value], ( end ( $return_string ) === FALSE ? 0 : ( key ( $return_string ) + 1 ) ) ) : $close_no_open[$code_value][] = ( end ( $return_string ) === FALSE ? 0 : ( key ( $return_string ) + 1 ) ) );[/php]

Instead of this…
[php]if ( isset ( $close_no_open[$code_value] ) === TRUE )
{
if ( end ( $return_string ) === FALSE )
{
$next_key = 0;
}
else
{
$next_key = ( key ( $return_string ) + 1 );
}

array_unshift ( $close_no_open[$code_value], $next_key );

}
else
{
if ( end ( $return_string ) === FALSE )
{
$next_key = 0;
}
else
{
$next_key = ( key ( $return_string ) + 1 );
}

$close_no_open[$code_value][] = $next_key;

}
[/php]

In this case is the Ternary Operator just used because the code writer was trying to make their code “hard to read”?

TIA
John

Yes.

As to why someone would write a nested ternary: inexperience, believing they are smart, masochist, egotistical, or just an idiot.
Ternaries should never be nested. You also don’t need the isset() === TRUE it just bloats the code, which also leads me to lean towards inexperience when, if(isset()) does the exact same thing.

Sponsor our Newsletter | Privacy Policy | Terms of Service