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));
Related
This is probably really basic and my first PHP question on here but here goes...
I'm working with WordPress and trying to output ACF data assigned to the current user.
So far I have:
<?php $current_user = wp_get_current_user();
echo the_field('price_list', 'user_$current_user->ID'); ?>
So basically, the output of wp_get_current_user is e.g. 2, but ACF needs to be passed the value as e.g user_2... so my question is how can I prepend the user_ to the 2?
I'd rather add it in at the output stage rather than adding it to the variable if that makes sense, as the variable is used elsewhere too.
You can use string concatenation:
echo "user_".$current_user->ID;
So in your example and specifications you would use it like this:
echo the_field('price_list', 'user_'.$current_user->ID);
I am making a dashboard using php and displaying it on webpage using html. For this I am taking some count values. Is !$value[" variable"] valid in php? How do i get the not of (!=) value? There is no other way to get the data. I am getting the values from a database.
print "<td><a href=\"$strBase and status!='Closed'\" target=\"_blank\">".$value[" ? "]."</td>";
I need to print !closed. Somehow I do not know how to pass that inside $value[].
If you want to use a variable as a key in order to retrieve a value from an array you can do as follows (I use a snippet of your code):
.$value[$myVar].
Where $myVar should be a string with the name of the key you want to retreive from $value
But anyhow, your question is pretty unclear so I'm not sure if this is what you are looking for.
You can check if the variable is empty(), i.e.:
if(empty($value["variable"])){
echo "variable is empty";
}
How to use a varible to store the value of the for loop?
for($i=1;$i<=$var;$i++)
How to get $i's value and store into another value and show its result?
I am a beginner of PHP and I want to improve my concept, I am very grateful if anyone can helps, Cheers!
Are you want dynamic variable or just a normal variable which will give you the out put like 12345678910
<?php
$var=10;
$a='';
for($i=1;$i<=$var;$i++){
$a.=$i;
}
echo $a;
?>
output: http://3v4l.org/2p8L6
or if you want dynamic variable then use following code to make a variable dynamic
${"num_" . $i} = $i;
output will be
$num_1=1;
$num_2=2;
$num_3=3;
etc
In plain English; PHP is updating the value stored in lopping variable automatically, and this is the whole idea behind using for loops, not only in PHP, but also in other programming languages.
Usually, you would want to use it (the variable $i) as a kind of a counter, and run code in the loop based on the iteration count.
How do I send in a "multi"-string variable(looking like this: 1,2.3,4) into a php through a parameter which goes through an url.
Lets say some of the php that is connected to the mysql server looks something like this
`$id_ = $_GET['id'];`
`$q=mysql_query("SELECT * FROM brain WHERE braincell_id in ('$id_');`
And the variable that goes into it is: id = 1,2,3,4
How can I make this work?
What I am using currently and that does work:
$q=mysql_query("SELECT * FROM brain WHERE braincell_id in ("$_GET['id']");
EDIT.
Sorry it's me being stupid, it is way to late for me to be doing this stuff..... I missed a parentheses please remove this post.
Pass $q, your result resource. It references all the data returned from MySQL, and you can use the usual mysql_* functions on it (eg. mysql_fetch_array()).
Alternatively, call mysql_fetch_array() (or similar) and pass the resultant array to your function.
The only solution that I know that will work out of the box is to pass each element of array as an separate parameter.
Here is my example:
get.php:
$arr = $_GET['id'];
echo '<ul>';
foreach($arr as $id)
{
echo "<li>{$id}</li>";
}
echo '</ul>';
URL:
get.php?id[]=1&id[]=2&id[]=3
Also you can pass key for each element:
get.php?id[0]=1&id[1]=2&id[2]=3
I'm trying to use some simple AJAX and then passing my variable through a HTML string.
Struggling to get some PHP included to print the value into the string. Been trying like this and the variable prints blank even though there is a value assigned to $user_id.
xmlhttp.open("GET","update.php?user=<?php $user_id ?>&q="+str,true);
Any suggestions on how I could do this or better solutions.
Thanks
you forgot echo:
xmlhttp.open("GET","update.php?user=<?php echo $user_id; ?>&q="+str,true)