this is the most strange thing that has ever happened to me with php
This is the code source code:
echo 'rsi_color_by_number '.$i+$di.' = rsicoloredbackground >'.$i+$di .' and rsicoloredbackground <'. $i+$di+1 ."? black:#ffffff00 \n";
and it echoes only this:
1? black:#ffffff00
The problem is operator precedence. + and . have the same precedence, and left associativity, so
'string' . $i + $di . 'string2'
is treated as
(('string' . $i) + $di) . 'string2'
This will try to use 'string' . $i as a number and add that to $di.
You can solve this with explicit parentheses:
echo 'rsi_color_by_number '.($i+$di).' = rsicoloredbackground >'.($i+$di) .' and rsicoloredbackground <'. ($i+$di+1) ."? black:#ffffff00 \n";
But when you find yourself repeating $i + $di in the code, it can be useful to assign it to a variable:
$newi = $i + $di;
echo 'rsi_color_by_number '.$newi.' = rsicoloredbackground >'.$newi.' and rsicoloredbackground <'. ($newi+1) ."? black:#ffffff00 \n";
If $i and $di are strings, try this:
echo 'rsi_color_by_number '.$i.$di.' = rsicoloredbackground >'.$i.$di .' and rsicoloredbackground <'. $i.($di+1) ."? black:#ffffff00 \n";
If $i and $di are numbers, try this:
$num = (int)$i + (int)$di;
echo "rsi_color_by_number {$num} = rsicoloredbackground > {$num} and rsicoloredbackground < ". (int)$num + 1 . "? black:#ffffff00 \n";
If that works for you, you should do calculations before your echo statement, to keep your code clean and precise and prevent against repeating the same calculation more often and making mistakes... the (int) before any variable will cast that variable as an integer; whereas before it is uncasted... Check php.net for type casting.
Without seeng what $i and $di is, it's difficult to know what you are doing.
If you don't know the values of those variables, don't forget to use var_dump
var_dump($i);
var_dump($di);
Related
I want to update the variable outside of the foreach scope and use it again in the condition inside it, but the variable in condition stays the same with the initial value. It gets updated outside, but the condition inside still uses the old value for comparing. How can the variable inside, used in condition get updated as well?
$total = 5.00000008;
for($i = 0; $i < count($values); $i++) {
if($total == $values[$i]){
$total += 0.00000001;
}
}
I am referring to the $total variable inside if condition, it doesn't get updated.
In a comment, #NanThiyagan wrote:
"your question was not clear.but your code works fine refer
eval.in/1050113"
Check out the output. It says 5.0000001. This might give you a hint that php automatically does something to round up your value.
Read this: http://php.net/manual/en/language.types.float.php
And pay attention to the part:
"So never trust floating number results to the last digit, and do not
compare floating point numbers directly for equality."
In this article they approach the problem with an implicit precision: https://www.leaseweb.com/labs/2013/06/the-php-floating-point-precision-is-wrong-by-default/
Like this:
ini_set('precision', 17);
echo "0.1 + 0.2 = ". ( 0.1 + 0.2 ) ."\n";
$true = 0.1 + 0.2 == 0.3 ? "Equal" : "Not equal";
echo "0.1 + 0.2 = 0.3 => $true\n";
I think you might not be fully aware of what's happening inside the loop.
The variable $total is being updated every time the condition is true. And the condition variable $total is updated as well.
Here's an example so you can see it happening:
$values = [5.00000008, 5.00000009, 5.00000007];
$total = 5.00000008;
for($i = 0; $i < count($values); $i++) {
echo ((string) __line__ . ' => ' . (string) $total . " (current total)\n");
if($total === $values[$i]){
echo ((string) __line__ . ' => ' . (string) $total . " (before increment)\n");
$total += 0.00000001;
echo ((string) __line__ . ' => ' . (string) $total . " (after increment)\n");
}
}
And here's the code tested: https://3v4l.org/EJkNG
I am referring to the $total variable inside if condition, it doesn't get updated.
Of course it gets updated.
Simply echo $total inside the loop to find out.
<?php
$values = [5.00000007, 5.00000008, 5.00000009];
$total = 5.00000008;
for ($i = 0; $i < count($values); $i++) {
if ($total == $values[$i]) {
$total += 0.00000001;
}
echo $total . "\n";
}
Output:
5.00000008
5.00000009
5.0000001
See for yourself at https://3v4l.org/RBHa3
For one of our projects we need a value input that supports number localization in IE, and as can be seen here (link) IE doesn't support comma separation. To work around this issue I figured to make a dropdown that has all values with commas.
I've run into a strange problem in automatically generating this range. It works for all values except 0. I hope there is a solution out there (or a logical explanation) to solve or help me understand this strange behaviour.
The code I use:
$select_number = "";
for ($i = 10; $i >= 0; $i -= 0.1){
$value = str_replace(".", ",", $i );
$select_number .= "<option value='" . $value . "' $selected>" . $value . "</option>";
}
this code is placed in the interface with
<select class="form-control" id="myname" data-live-search="true" name="myname">
<?php echo $select_number; ?>
</select>
This renders the the following dropdown
Notice that 0 became 1,87905246918E-14. If I change the order of the for loop to for($i = 0; $i <= 10: $i += 0.1) everything works fine... Anybody know a solution to this?
I use php version 5.6.28
Loop through the integers instead:
$select_number = "";
for ($i = 100; $i >= 0; $i -= 1){
$value = str_replace(".", ",", $i/10 );
$select_number .= "<option value='" . $value . "' $selected>" . $value . "</option>";
}
Floating-point numbers suffer from round-off error.
I have a Foreach where I have two variables ($num1 and $num2) as string. I'd like to covert them as number (integer should be ok).
This is my code:
...
foreach ($titles as $match) {
list($num1, $num2) = explode(':', $results[$c++]->innertext); // <- explode
echo "<tr><td class='rtitle'>".
"<td class='last-cell'>".$match_dates[$c]->innertext . "</td> " .
//"<td class='first-cell tl'>".$match->innertext."</td> " .
" - ".$match->innertext." ".$num1.':'.$num2 . " " .
"<td class='odds'>".$best_bets[$b++]->attr['data-odd'] . ";" .
"".$odds[$b++]->attr['data-odd'] . ";" .
"".$odds[$b++]->attr['data-odd'] . "</td>" .
"</td></tr><br/>";
}
...
Thanks!
EDIT
Thanks for your answer. I am going to explain and maybe you could help me: I have a a problem with best_bets value, because it's not alway before odds variable, so I was thinking to use "if statement", where I can fix some rules: when $num1 is > $num2, I will show an echo order; when $num1 is = $num2 another echos order and. at least, $num1 is < $num2 another echos order. But to do this, I need to convert $num1 and $num2 to do it. I hope to be clear. Thank you for your helping
You could cast them to an int:
$num1 = (int) $num1;
Or use the intval function:
$num1 = intval($num1);
Hope this helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to add something to PHP's loop variable and check the result? I have this simple code:
<?php
for ($i=0; $i<5; $i++) {
echo "Time: " . $i + 1 . "<br/>";
}
?>
The displayed result in the page:
1
1
1
1
1
But if there's no addition with 1, the result is correct. How to do this addition?
You concat in your question use this:
<?php
for ($i=0; $i<5; $i++) {
echo "Time: " . ($i + 1) . "<br/>";
}
?>
Change this line:
echo "Time: " . $i + 1 . "<br/>";
to this:
echo "Time: " . ($i + 1) . "<br/>";
Put the calculation in brackets:
<?php
for ($i=0; $i<5; $i++) {
echo "Time: " . ($i + 1) . "<br/>";
}
?>
While adding parentheses to give the addition a higher precedence is an option, the easiest way is to turn $i + 1 into an expression, to be resolved to a singular value and then pass it to echo, using comma's:
for ($i=0; $i<5; $i++)
{
echo "Time: " , $i + 1 , "<br/>";
}
But the shortest way to write this is:
for ($i=0;$i<5;)//don't increment here
{
echo 'Time: ', ++$i, '<br/>';//increment here, and make sure to pre-increment
}
Or, more readable (and IMHO therefore better):
for($i=1;$i<6;$i++)
{//start with one, so you needn't increment in the loop body!
echo 'Time: ', $i, '<br/>';
}
Leaving out conactenation is marginally faster, but you'll hardly notice that. It's just an alternative. Think of the two options as this (if you're familiar with C++):
int i = 0;
std::stringstream toPrint;//create string-stream
toPrint << "Time: " << ++i << "<br/>";//pass substrings/int chunks to stream
std::COUT << toPrint.str();//create single string, and pass to output-stream
But that's just silly, considering you're actually creating a stream, just to pass your substrings/ints to, only to pass the resulting string to the output stream. You might as well pass all chunks to the output stream directly:
std::<COUT << "Time: " << ++i << "<br/>"; //pass string, int and string to output
Think of echo as a language construct (which it is) that is your access-point to the STDOUT stream. Whe concatenate a string manually, of you can just pass it to the stream as-is? without any overhead?
As an asside, you can get the output you need in just 2 lines of code, without (explicitly) looping and incrementing a variable:
$vals = range(1,5);//create array 1, 2,3, 4, 5
//repeat format Time: %d<br/> for every index in $vals
vprintf(str_repeat('Time: %d <br/>', count($vals)), $vals);
//pass resulting format to vprintf, allong with the array of values
The output is the same. In case you're thinking about using this: don't. It's just for fun, and an example of how you can take compacting code a bit too far
<?php
for ($i=1; $i <= 5; $i++) {
echo "Time: " . ($i) . "<br />";
}
You are looking to do something like this :
<?php
for ($i=0; $i<5; $i++) {
$j=$i+1;
echo "Time: " . $j . "<br/>";
}
?>
Like DanFromGermany suggested, why not start with $i one higher than currently?
Also if you have to do calculations, the meaning of the var usually differs from it's starting point. So for readability and reusability do what Munjal proposed, but leave out the concatenation;
<?php
for ($i=0; $i<5; $i++) {
$laterTime = $i + 1;
echo "Time: {$laterTime} <br />\n";
}
?>
<?php
for ($i=0; $i<5; $i++) {
$i++;
echo "Time: ".$i. "<br/>";
}
?>
or if you insist on concatenation
<?php
for ($i=0; $i<5; $i++) {
echo "Time: " .++$i. "<br/>";
}
?>
Is there a speed difference between, say:
$newstring = "$a and $b went out to see $c";
and
$newstring = $a . " and " . $b . " went out to see " . $c;
and if so, why ?
Depending on the PHP version, it varies by how much the second is faster if you write it like:
$newstring = $a . ' and ' . $b . ' went out to see ' . $c;
PHP is very inconsistent from version to version and build to build when it comes to performance, you have to test it for yourself.
What nees to be said is that it also depends on the type of $a, $b and $c, as you can see below.
When you use ", PHP parses the string to see if there are any variable/placeholders used inside of it, but if you use only ' PHP treats it as a simple string without any further processing. So generally ' should be faster. At least in theory. In practice you must test.
Results(in seconds):
a, b, c are integers:
all inside " : 1.2370789051056
split up using " : 1.2362520694733
split up using ' : 1.2344131469727
a, b, c are strings:
all inside " : 0.67671513557434
split up using " : 0.7719099521637
split up using ' : 0.78600907325745 <--- this is always the slowest in the group. PHP, 'nough said
Using this code with Zend Server CE PHP 5.3:
<?php
echo 'a, b, c are integers:<br />';
$a = $b = $c = 123;
$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
$newstring = "$a and $b went out to see $c";
$t = xdebug_time_index() - $t;
echo 'all inside " : ', $t, '<br />';
$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
$newstring = $a . " and " . $b . " went out to see " . $c;
$t = xdebug_time_index() - $t;
echo 'split up using " : ', $t, '<br />';
$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
$newstring = $a . ' and ' . $b . ' went out to see ' . $c;
$t = xdebug_time_index() - $t;
echo 'split up using \' : ', $t, '<br /><br />a, b, c are strings:<br />';
$a = $b = $c = '123';
$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
$newstring = "$a and $b went out to see $c";
$t = xdebug_time_index() - $t;
echo 'all inside " : ', $t, '<br />';
$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
$newstring = $a . " and " . $b . " went out to see " . $c;
$t = xdebug_time_index() - $t;
echo 'split up using " : ', $t, '<br />';
$t = xdebug_time_index();
for($i = 1000000; $i > 0; $i--)
$newstring = $a . ' and ' . $b . ' went out to see ' . $c;
$t = xdebug_time_index() - $t;
echo 'split up using \' : ', $t, '<br />';
?>
There likely will be a speed difference, since it's two different syntaxes. What you need to ask is if the difference is important. In this case, no, I don't think you need to be worried. The difference would be too negligible.
I would recommend you doing whatever makes most sense to you visually. "$a and $b went out to see $c" can be a bit confusing when looking at it. If you wanted to go that route, I'd suggest curly-braces around your variables: "{$a} and {$b} went out to see {$c}".
I did a quick benchmark, and as others have said, the results were very inconsistent. I didn't notice any performance gain using single quotes instead of double ones. My guess is that it all comes down to preference.
You may want to stick to one type of quote for your coding style, and if you do, choose double quotes. The replacement feature comes in handy more often than you'd think.
I put the benchmark code on github.
If you're concerned about the speed of string concatenations at this level, you are using the wrong language. Compile an application in C for this use case and call that in your PHP script, if this really is a bottleneck.
Yes there is, however the difference is very negligible between
$newstring = "$a and $b went out to see $c";
and
$newstring = $a . " and " . $b . " went out to see " . $c;
If you used:
$newstring = $a . ' and ' . $b . ' went out to see ' . $c;
The difference would be slightly bigger (but probably still negligible), the reason for this is, if I recall correctly (I may be wrong on this), that PHP scans and parses the contents within double quotation marks for variables and special characters (\t, \n and so on) and when using single quotation marks it doesn't parse for variables or special characters, so there may be a slight increase in speed.
Why don't you test it, and compare the difference? Numbers don't lie, if you find that one performs better than the other, then you should ask why.
there is NO difference, period. ;)