We all are familiar with PHP variables. PHP Variables are used for storing data and that can be constructed using alphabets, numbers, underscore and dot symbol. You might have heard about Dynamic variables. If so, what is the purpose of making dynamic names for variables. Let's find it out.
Here is an array containing information about 3 cricket players
We are going to display this information in a table format. We need to show a bat icon if player is a batsman and ball icon if player is a bowler and so on. Following variables keeps the urls of image icons.
Creating an html table to show the players information.
The image variable is constructed with values from the information array. if the value of $cricket_player[2] is 'batsman' then ${"img_".$cricket_player[2]} will converted to $img_batsman
Browser will get "/images/bat_icon.jpg" as image url and will display the bat icon.
I think you got me. Include dynamic variables in your code, which will be helpful in Minimizing it.
Here is an array containing information about 3 cricket players
$cricket[0]= ['1011','Mahendra Singh Dhoni', 'batsman']; $cricket[1]= ['1012','Mohammed Shami', 'bowler']; $cricket[2]= ['1013','Ravindra Jadeja', 'allrounder'];
We are going to display this information in a table format. We need to show a bat icon if player is a batsman and ball icon if player is a bowler and so on. Following variables keeps the urls of image icons.
$img_batsman = "/images/bat_icon.jpg"; $img_bowler = "/images/bowl_icon.jpg"; $img_batsman = "/images/cap_icon.jpg";
Creating an html table to show the players information.
<table> <tr> <th>Image</th> <th>Name</th> <th>Type</th> </tr> <?php foreach($cricket as $cricket_player){ ?> <tr> <td> <img src="<?php echo ${"img_".$cricket_player[2]}; ?>" /> </td> <td> <?=$cricket_player[1]?> </td> <td> <?=$cricket_player[2]?> </td> </tr> <?php } ?> </table>
The image variable is constructed with values from the information array. if the value of $cricket_player[2] is 'batsman' then ${"img_".$cricket_player[2]} will converted to $img_batsman
Browser will get "/images/bat_icon.jpg" as image url and will display the bat icon.
I think you got me. Include dynamic variables in your code, which will be helpful in Minimizing it.
0 comments:
Post a Comment