php - error in viewing variable and function -


i have code :

<?php  function random(){ echo rand(0,50); };  ( $x = 1; $x <= 20; $x++){     echo $x." : ".random()."<br>"; };  ?> 

and outputs

231 :
232 :
93 :
84 :
15 :

it should :

1 : 23
2 : 23
3 : 9
4 : 8
5 : 1

this because of echo rand(0,50);. need use return rand(0,50);, see below:-

<?php  function random(){ return rand(0,50); };  ( $x = 1; $x <= 20; $x++){     echo $x." : ".random()."<br>"; };  ?> 

output:- https://eval.in/397980

note:- correct explanation given @mark baker:-

echo rand(0,50);will going executes before concatenation, , hens output first , $x value. random function return value can concatenated echo statement inside loop


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -