"A non well formed numeric value encountered in" with while loop - php

I'm setting up a while function that adds four every time it runs. But I get this weird error.
Notice: A non well formed numeric value encountered in.
I would like to fix this issue only I couldn't find any info regarding this problem in the instance of a while loop. Thank you in advance.
$x= 2012;
while($x <= 2084){
echo "Schrikkeljaar:" . $x+=4 ."<br>";
}

Wrap it in parenthesis:
echo "Schrikkeljaar:" . ($x += 4) . "<br>";
$x= 2012;
while($x <= 2084){
echo "Schrikkeljaar:" . ($x += 4) . "<br>";
}
Live Example
Repl

Php is trying to add "4<br>" to x. The easiest way to solve this is by adding that before:
$x= 2012;
while($x <= 2084){
$x+=4;
echo "Schrikkeljaar:$x<br>";
}

Related

While loop hangs when decrement variable is set in PHP

the while loop hangs with the decrement variable ($j--) set after the echo command..
$j = 10;
while($j>-10)
{
if($j==0)continue;
echo (10/$j) . "<br>";
$j--;
}
but works well when set before if statement
$j = 10;
while($j>-10)
{
$j--;
if($j==0)continue;
echo (10/$j) . "<br>";
}
Can anyone explain pls?
When $j==0 it skips the rest of the code in the loop because of the continue so it never gets to the $j-- in the first code.
This means that $j will never get below 0 and the loop will never finish.
As in the second code it will always decrements it before the test, it will eventually get to -10.
Try adding
echo $j.PHP_EOL;
as the first line in the loop (assuming you are using the CLI version) to see what is happening.
Another slightly different version of your code can be used to ONLY avoid the echo when $j is 0, instead of doing the continue...
$j = 10;
while($j>-10)
{
if($j != 0) {
echo (10/$j) . "<br>";
}
$j--;
}

For loop isn't running with php post

I'm converting a bash cgi script to php on apache webserver. For whatever reason I have a for loop that doesn't want to execute. I've tried comparing it to other loops I've done in the past and it seems like it should work but nothing shows up on the page in the source code. It's just an empty area. I also put an echo in the code to echo out which number it's on, but those didn't show up either. Anybody see where I'm going wrong?
for($i=$begin; $i>=$end; $i++) {
echo "$i\n";
echo "<td>somedata$i</td></tr><tr>\n";
}
I've already verified the variables begin and end show up by echoing them just before the loop. Once I hit the loop it's not executing that code. Everything after the loop executes fine as well. I'm also not getting any errors in the apache log files, which is what really is frustrating. Any help would be appreciated.
Thanks!
I think that you should replace >= to <=
i supposed that
$begin =5;
$end = 0;
then you should used decrements $i-- with >=
for($i=$begin; $i>=$end; $i--) {
echo "$i\n";
echo "<td>somedata$i</td></tr><tr>\n";
}
if
$begin = 0;
$end = 5;
then you should less than equal to <= with increment $i++
for($i=$begin; $i<=$end; $i++) {
echo "$i\n";
echo "<td>somedata$i</td></tr><tr>\n";
}
I am not sure about the values both the variable have, but you need to change the >= sign to <= so that the loop can go until $i goes up to the $end value.
for($i=$begin; $i<=$end; $i++) {
echo "$i\n";
echo "<td>somedata$i</td></tr><tr>\n";
}
Your code is use incremental for $i and in your logic you use >=$end I think you have to change it to $i
for($i=$begin; $i<$end; $i++) { echo "$i\n"; echo "<td>somedata$i</td></tr><tr>\n";
Change either >= to <= or make it a decrement loop by changing i++ to i-- Both will work.

PHP - Simple for loop not working? No errors returned

I'm currently having trouble printing some simple test statements within my for loop.
<?php
include('../connstr.inc');
$email=$_REQUEST["email"];
$datafile=$_REQUEST["datafile"];
$email_safe=preg_replace("/[^a-zA-Z]/","_",$email);
$path="../uploaded_data";
$xml = simplexml_load_file("{$path}/{$email_safe}/{$datafile}.xml");
// Retreive data details for specified activity
$lapcount = $xml->Activities->Activity->Lap->count();
echo "LapCount: " . $lapcount;
$totalTime = array(); $distance = array(); $maxSpeed = array();
$calories = array(); $intensity = array(); $trigMethod = array();
echo 'Test1';
// Collect details for each lap
for($x = 0; $x < $lapCount; $x++) {
echo 'Test2';
// Find how many trackpoints exist for specified lap
$trackPointCount = $xml->Activities->Activity->Lap[$x]->Track->Trackpoint->count();
echo 'Test3';
echo "<br /> Lap {$x} TrackP Count: " . $trackPointCount;
echo 'Test4';
}
When I run it, only 'Test1' and the 'lapCount' gets printed. Anything within the for loop doesn't run. No errors are being returned either. $x will definitely be less then $lapCount as this is just 3. I fail to see the (most likely) stupid mistake I've made even after looking over it many times.
The problem with your code is this:
You declare lapcount as $lapcount, but try using it as $lapCount
(Notice the capitalization of the "C")
Ensure all uses of this variable are typed out exactly the same and everything will work
EDIT:
Seems like you discovered this as I posted my answer
Initially you are using variable name $lapcount but in the last for loop you are using $lapCount that is the reason why you are not getting any value. Use $lapcount in the last for loop also.
for($x = 0; $x < $lapcount; $x++) { // use $lapcount
echo 'Test2';
$trackPointCount = $xml->Activities->Activity->Lap[$x]->Track->Trackpoint->count();
echo 'Test3';
echo "<br /> Lap {$x} TrackP Count: " . $trackPointCount;
echo 'Test4';
}
Here, the name of your var is $lapcount without 'C' lowercase:
// Retreive data details for specified activity
$lapcount = $xml->Activities->Activity->Lap->count();
echo "LapCount: " . $lapcount;
but here, you use 'C' uppercase, $lapCount:
for($x = 0; $x < $lapCount; $x++) {

Using dynamic variables with isset()?

I have a loop that has a dynamic variable in it, eg:
while(i < 10){
echo ${"dynamic" . $i . "var"};
$i++;
};
I want to only echo the variable if the original var (say $dynamic3var) is set so I add:
while(i < 10){
if(isset(${"dynamic" . $i . "var"})){
echo ${"dynamic" . $i . "var"};
$i++;
};
};
However this wont work as its still picking up $i.
Does anyone know a correct way of doing this?
Since global variables are bad ideas you should rethink your code. A plain refactoring would be to use an associative array (even if it remains a global variable at the first step). Then you could work with
if( isset($dynamic[$i]) ) ...
Why are Globals evil? Read this: http://tomnomnom.com/posts/why-global-state-is-the-devil-and-how-to-avoid-using-it
Try this:
while($i < 10){
$label = "dynamic".$i."var";
if(isset($$label))
echo $$label;
$i ++;
};

Run A PHP function only 30% of Time

I need help creating PHP code to echo and run a function only 30% of the time.
Currently I have code below but it doesn't seem to work.
if (mt_rand(1, 3) == 2)
{
echo '';
theFunctionIWantCalled();
}
Are you trying to echo what the function returns? That would be
if(mt_rand(1,100) <= 30)
{
echo function();
}
What you currently have echoes a blank statement, then executes a function. I also changed the random statement. Since this is only pseudo-random and not true randomness, more options will give you a better chance of hitting it 30% of the time.
If you intended to echo a blank statement, then execute a function,
if(mt_rand(1,100) <= 30)
{
echo '';
function();
}
would be correct. Once again, I've changed the if-statement to make it more evenly distributed. To help insure a more even distribution, you could even do
if(mt_rand(1,10000) <= 3000)
since we aren't dealing with true randomness here. It's entirely possible that the algorithm is choosing one number more than others. As was mentioned in the comments of this question, since the algorithm is random, it could be choosing the same number over, and over, and over again. However, in practice, having more numbers to choose from will most likely result in an even distribution. Having only 3 numbers to choose from can skew the results.
Since you are using rand you can't guarantee it will be called 30% of the time. Where you could instead use modulus which will effectively give you 1/3 of the time, not sure how important this is for you but...
$max = 27;
for($i = 1; $i < $max; $i++){
if($i % 3 == 0){
call_function_here();
}
}
Since modulus does not work with floats you can use fmod, this code should be fairly close you can substitute the total iterations and percent...
$total = 50;
$percent = 0.50;
$calls = $total * $percent;
$interval = $total / $calls;
$called = 0;
$notcalled = 0;
for($i = 0; $i <= $total; $i++){
if(fmod($i, $interval) < 1){
$called++;
echo "Called" . "\n";
}else{
$notcalled++;
echo "Not Called" . "\n";
}
}
echo "Called: " . $called . "\n";
echo "Not Called: " . $notcalled . "\n";

Categories