简体   繁体   中英

How to store a echo result into a variable in php?

I am a new bee to this. I am trying to store the result of a random value generated into a variable which then I will be storing in MySQL DB.

Tried a code like below

 $des_nu1 = echo rand(100,9999);

but looks like the statement isn't correct. Please advise.

I did see an option to use session but is there any alternative than using session ( Ref using Session : How to store a variable in php using session )

Remove the echo

 $des_nu1 = rand(100,9999);

The only thing echo does is this Output one or more strings . Since you don't want to output the result of your function you don't need echo. After you assign that value to your variable you can use it anyway you want and you can also use it for echo

 echo $des_nu1;
 $temp = $des_nu1 + 10; //etc

Always refer to the manual,
echo is language construct echo manual that returns nothing, it will write in standard output (stdout)

I advise you to always use sprintf()

sprintf("%i", rand(100,9999));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM