how to get a variable value that will be changed later - php

i have this code
$number = 1;
echo $number;
for ($i=0; $i < 10; $i++) {
$number++;
}
the output of echo $number is 1 not 11.
How can get the last $number value when I called it before it changed?

Once you output something to the browser, it's done. You cannot change it again later. The only way to handle this is to not output the variable until you have found its final value; ie in the example you move the echo statement to the bottom.
It's generally considered a good idea to first run all of your PHP code and determine all your variables and only then start outputting things to the browser in order to prevent the kind of problem you have now.

$number = 1;
echo 'before increment :'.$number;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo 'after increment :'.$number;

Try this way, now you will get expected result:
$number = 1;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo $number;
Reason, you have put echo $number before the increment, which was logically wrong:

Related

Exam Qn: Convert do while loop to for loop (PHP)

Recently, my exams got over. My last exam was based on PHP. I got the following question for my exam:
"Convert the following script using for loop without affecting the output:-"
<?php
//Convert into for loop
$x = 0;
$count = 10;
do
{
echo ($count. "<BR>");
$count = $count - 2;
$x = $x + $count;
}
while($count < 1)
echo ($x);
?>
Please help me as my computer sir is out of station and I am really puzzled by it.
Well, If I understand well, You have to use "for" loop, instead of "do...while", but the printed text must not change.
Try:
$count = 10;
$x = 0;
$firstRun = true;
for(; $count > 1 || $firstRun;) {
$firstRun = false;
echo ($count . "<BR>");
$count -= 2;
$x = $x + $count;
}
echo ($x);
By the way loop is unnecessary, because $count will be greater than 1 after the first loop, so the while will get false.
EDIT
$firstRun to avoid infiniteLoop
$count in loop
EDIT
Fixed code for new requirement
Removed unnecessary code
Hmmm I don't know if your teacher wanted to own you... but the do{} will execute only once since $count is never < 1.
The output of your teacher's code is:
10
8
I presume there was a mistake in the code and the while would be while($count > 1) which would make more sense (since it's weird to ask for a loop to output only 10 8) and would result in this output:
10
8
6
4
2
20
Then a good for() loop would have been :
$x = 0;
$count = 10;
for($i = $count; $i > 1; $i -= 2)
{
$count -= 2;
echo $i . "<br>";
$x += $count;
}
echo $x;
Which will output the same values.
If you can, ask your teacher for this, and comment the answer ^^ ahahah

PHP echo line X times different text

Title might not make complete sense, couldn't think of a way to explain it so I will do a mock PHP example.
<?php
$startNumber = 1;
$endNumber = 15;
$fileExt = '.jpg';
?>
And then on the page it will echo from the $startNumber to the $endNumber in something like a foreach
<?php echo("<img src=\"#.jpg\">"); ?>
Hope this is making sense...
I'd also like to point out that the numbers < 10 start with a 0 so it will need to be in the image source link.
echo is not a function, it's a language construct. Don't use ().
Also you need to use a loop like for() to do that (Also see sidenote of Fred -ii).
$endNumber = 15;
for($i = 1; $i <= $endNumber; $i++) {
if($i < 10) {
$i = "0".$i;
}
echo "<img src=\"".$i.".jpg\" />";
}
If this is not what you asked, add more information.

How to get a total value from a loop result for integers in php. without Javascript of Jquery

how to add a total of the numbers displayed as an integer from a loop. the loop result is not an increment. here is the problem.
<?php
$i = 0;
$num =1;
while($i<=4){
echo $num;
$i++;
}
echo $num;
?>
So the result is something like this.
1 1 1 1
so my problem is how can i total the result which should be 4, and save it in a variable without incrementing. and even more confusing is. how to do the same when the value of $num is dynamic. Thank you very much. in advance
Just make an array then sum them:
<?php
$i = 0;
$num =1;
while($i<=4){
$nums[] = $num;
$i++;
}
echo array_sum($nums); //Outputs 5
?>
This assumes that $num is always numeric.
Alternatively, you could just iterate an output variable, based on the value of $num. Like so:
<?php
$num = 1; // or 2, or 3, or 4 or whatever
$output = 0;
for ($i=0; $i<=4; $i++) {
$output += $num;
}
echo $output;
?>

there's any way to use foreach with AND?

foreach(($_POST["msg"] as $mg) AND ($_POST["control"] as $id))
{
echo $mg;
echo $id;
}
i need make something like that, any way to do? i'm trying to get 10 mysql records and edit all of them
No, that won't work. The closest thing I can see to what you're trying to do is:
for($i = 0; $i < count($_POST["msg"]); $i++) {
echo $_POST["msg"][$i];
echo $_POST["control"][$i];
}
Assuming that "msg" and "control" will always contain the same amount of items.
Assuming both $_POST['msg'] and $_POST['control'] are actually arrays, have numeric keys (thanks #iMoses), and have the same length, you could use a for loop -
for ($i = 0; $i < count($_POST["msg"]); $i++){
$mg = $_POST['msg'][$i];
$id = $_POST['control'][$i];
}

0's Not being Stripped from the front of a numeric value

I am working on a project for a friend's website which is suppose to generate completely random phone numbers to be displayed on a "fake" review board. I figured the best way to do with would be for me to to generate out each section separably. So 3-3-4, but no matter what I do, every time there is a 0 in front the code cuts it off. Here's an example of what I mean:
http://www.shiningashes.net/Test.php
yet this is what I have for the code:
<?php
for ($i = 0000; $i <= 9999; $i++) {
echo $i;
echo "<br>";
}
?>
How do I get the 0's to stop being cropped out so the 0's display? 0001, 0021, 0123, etc?
You can use str_pad
for ($i = 0; $i <= 9999; $i++) {
echo str_pad($i, 4, '0', STR_PAD_LEFT);
echo "<br>";
}
You can use printf to format your output:
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br>\n",$i);
}
?>
You need to make your variable a string if you want to keep the zeros. That would mean using quotes, and never using numeric operators on it. But since you depend on using ++ on it, I suggest the following hack:
<?php
for ($i = 10000; $i <= 19999; $i++) {
$str=substr ( $i , 0 , 4 );
echo $i;
echo $str;
echo "<br>";
}
?>
You will need to convert your integer to a string when printing it.
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br />", $i);
}
?>
Check the documentation for printf/sprintf for more information.
Kind regards,
Stefan

Categories