PHP Assistance with empty() in a loop -
let's wanted check see if variable empty , something... can this:
if ( empty($phone) ) { $phone = 'not provided'; }
but want bunch of items. i'm thinking array , loop, this:
$optionalfieldsarray = array($phone, $address, $city, $state, $zip); foreach ($optionalfieldsarray $value) { //what goes here???? }
is foreach resonable way it, check if $phone, $address, $city, etc. empty , assign "not provided" string when is?
if so, can me syntax goes inside loop?
you can this:
<?php $required_vars = array( 'phone', 'address', 'city', 'state', 'zip' ); foreach( $required_vars $required_var ) { if( empty( $$required_var ) ) $$required_var = 'not provided'; // $$var -> variable name = value of $var } ?>
check above code yourself. can understand how works. because confusing concept.
Comments
Post a Comment