php - error in viewing variable and function -
this question has answer here:
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
Post a Comment