php - Correct method of checking with an array has elements -


i trying check returned array exists before accessing elements. @ first had

$img = wp_get_attachment_image_src($img_id, 'type')[0]; 

but since array didn't exist, changed

$check = wp_get_attachment_image_src($img_id, 'type'); if ($check) {     $img = wp_get_attachment_image_src($img_id, 'type')[0]; } 

despite check (which same wordpress recommendation below) still error within if statement

parse error: syntax error, unexpected '['

here wordpress example usage below. cannot see why mine different. have tried !empty($check) , number of other things. silly mistake making?

<?php  $attachment_id = 8; // attachment id  $image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns array if( $image_attributes ) { ?>  <img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"> <?php } ?> 

you can't append [] @ end of function calls php version below 5.4. first need assign variable function result.

$check = wp_get_attachment_image_src($img_id, 'type'); if ($check) {     $img = $check[0]; } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -