JasonDavis
JasonDavis

Reputation: 48933

What is this php supposed to do?

I found this code below in a script, I am trying to figure out if it is actually supposed to do more then I am seeing, As far as I can tell it results in an array of:

$maxSize[0] with $new_width
$maxSize[1] with $source_width

$maxSize = array($new_width ? $new_width : $source_width, $new_height ? $new_height : $source_height);

Upvotes: 0

Views: 124

Answers (5)

Daisy Moon
Daisy Moon

Reputation: 186

It creates a array with two elements. The first element is set the width.if there is a new width set, then it defaults to the source width. It same with the second element, setting the height.

Upvotes: 0

Steven Surowiec
Steven Surowiec

Reputation: 10220

It results in an array with 2 indexes. But it does 2 ternary comparison checks to see what those indexes should equal.

For the first one if $new_width has a value, it'll use that other wise it'll use $source_width.

For the second one if $new_height has a value it'll use that other wise it'll use $source_height.

This can be expanded as:

$maxSize = array();
if ($new_width)
  $maxSize[] = $new_width;
else
  $maxSize[] = $source_width;

if ($new_height)
  $maxSize[] = $new_height;
else
  $maxSize[] = $source_height;

Upvotes: 2

Sri
Sri

Reputation: 5845

$maxSize[0] will be equal to $new_width if $new_width exists, else $source_width
$maxSize[1] will be equal to $new_height if $new_height exists, else $source_ height

See this: http://en.wikipedia.org/wiki/Ternary_operation

Upvotes: 0

Dave
Dave

Reputation:

It's using inline if statments. If $new_width is set, it will use that value. Otherwise, it defaults to $source_width. The same is true for $new_height. And Yes, you do get a numerically keyed array with two values.

Upvotes: 3

Electro
Electro

Reputation: 3074

It creates an array with two elements. If $new_width is set and larger than zero, the first element will be $new_width. If not, it will be $source_width. The same applies to the latter, just with height. Read ternary comparison operator for more information.

Upvotes: 0

Related Questions