Jump to content

Improving a function with an isset(). Heredoc syntax....


sepoto

Recommended Posts

I have this code from another programmer:

 function getHours($time, $day, $close){  if(!$close){  $hours = <<<htmltext                            <option value="1" {$time[$day]['hours1']} >1</option>                          <option value="2" {$time[$day]['hours2']} >2</option>                          <option value="3" {$time[$day]['hours3']} >3</option>                          <option value="4" {$time[$day]['hours4']} >4</option>                          <option value="5" {$time[$day]['hours5']} >5</option>                          <option value="6" {$time[$day]['hours6']} >6</option>                          <option value="7" {$time[$day]['hours7']} >7</option>                          <option value="8" {$time[$day]['hours8']} >8</option>                          <option value="9" {$time[$day]['hours9']} >9</option>                          <option value="10" {$time[$day]['hours10']} >10</option>                          <option value="11" {$time[$day]['hours11']} ">11</option>                          <option value="12" {$time[$day]['hours12']} ">12</option>htmltext;}

It's good code but I need there to be an isset() happening somehow. I am currently working on the problem but I am not fully sure yet on the significance of the { }'s here and if I can just add an:

{ if(isset($time[$day]['hours1'])) echo $time[$day]['hours1']; }

I'm not sure if that direct change would work but I'm about to find out. I am trying to change the code as little as possible and I would prefer not to change my php.ini settings. I actually like being warned if my variables are not set (call me crazy!). Love this forum by the way! Thanks....

Link to comment
Share on other sites

So as I change the code to this I am left with an array to string conversion? Hmmm... I'm confused here:

<?php function getHours($time, $day, $close){  if(!$close){$hours = <<<htmltext <option value="1" { if(isset($time[$day]['hours1'])) echo $time[$day]['hours1'];} >1</option><option value="2" { if(isset($time[$day]['hours2'])) echo $time[$day]['hours2']; } >2</option><option value="3" { if(isset($time[$day]['hours3'])) echo $time[$day]['hours3']; }>3</option><option value="4" { if(isset($time[$day]['hours4'])) echo $time[$day]['hours4']; } >4</option><option value="5" { if(isset($time[$day]['hours5'])) echo $time[$day]['hours5']; } >5</option><option value="6" { if(isset($time[$day]['hours6'])) echo $time[$day]['hours6']; } >6</option><option value="7" { if(isset($time[$day]['hours7'])) echo $time[$day]['hours7']; } >7</option><option value="8" { if(isset($time[$day]['hours8'])) echo $time[$day]['hours8']; } >8</option><option value="9" { if(isset($time[$day]['hours9'])) echo $time[$day]['hours9']; } >9</option><option value="10" { if(isset($time[$day]['hours10'])) echo $time[$day]['hours10']; } >10</option><option value="11" { if(isset($time[$day]['hours11'])) echo $time[$day]['hours11']; } >11</option><option value="12" { if(isset($time[$day]['hours12'])) echo $time[$day]['hours12']; } >12</option> htmltext;} }?>

Notice: Array to string conversion in C:\xampp\htdocs\dailysportsguide\functions\f_hours.php on line 9

Edited by sepoto
Link to comment
Share on other sites

heredoc works like double quote. as everything inside double quote treated as string you cant use any expression or code block there but it can resolve variable names. {} curly braces is called interpolation when used inside string context with variable. to resolve an array element they have to be inside curly braces.more about them here http://php.net/language.type.string to test it is set it or not you have to do it outside heredoc.

Edited by birbal
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...