PHP comparison among strings in different php servers - php

I have an php in a server and i found an issue (solved). The issue was in a comparison among to long integer saved in a string and I can not use Equal, I have to use Identical. But I do not know why I have to use it, if it is a comparison among to strings.
I make a test, and I get two different results, in local host and server.
Server PHP version: 5.3.10-1ubuntu3.10
Local PHP version: 5.6.30-7+deb.sury.org~xenial+1
Here is mi code:
<?php
$a = "1285615000003961035";
$b = "1285615000003961023";
if($a == $b)
{
echo "$a == $b<br>";
}
else
{
echo "$a != $b<br>";
}
if((string)$a == (string)$b)
{
echo "(string)" . (string)$a . " == (string)" . (string)$b . "<br>";
}
else
{
echo "(string)" . (string)$a . " != (string)" . (string)$b . "<br>";
}
if($a === $b)
{
echo "$a === $b<br>";
}
else
{
echo "$a !== $b<br>";
}
if((string)$a === (string)$b)
{
echo "(string)" . (string)$a . " === (string)" . (string)$b . "<br>";
}
else
{
echo "(string)" . (string)$a . " !== (string)" . (string)$b . "<br>";
}
?>
In Server I get:
1285615000003961035 == 1285615000003961023
(string)1285615000003961035 == (string)1285615000003961023
1285615000003961035 !== 1285615000003961023
(string)1285615000003961035 !== (string)1285615000003961023
and in local:
1285615000003961035 != 1285615000003961023
(string)1285615000003961035 != (string)1285615000003961023
1285615000003961035 !== 1285615000003961023
(string)1285615000003961035 !== (string)1285615000003961023

Since PHP 5.4, according to the release notes, integral strings that overflow into floating point numbers will no longer be considered equal, because of the way float numbers are represented internally.
For more information see the links below:
http://php.net/manual/en/language.operators.comparison.php (Warning section after the examples)
http://php.net/manual/en/types.comparisons.php#108264

You should use strcmp or === for comparing strings (as you have used), because when you use == PHP will do type conversions.
http://php.net/manual/en/language.operators.comparison.php
Edit: As Lucas Mendes stated above, this is only partially true after PHP 5.4 - but still it is safer to use operators that do not do type conversions by default to compare strings.

Related

How to check `if` statement from database column like '$a > $b'

How to check if statement from database column like $a > $b or $a == $b or $a != $b or $a < $b
I got data from database in $row['condition']
$row['condition'] = '$a == $b';
or
$row['condition'] = '100 == 10';
in if statement I want to check
if($row['condition']){
echo 'true';
}else{
echo 'false';
}
I want to output
false
A way to do so is using eval() function. But as the manual says, this is very dangerous:
Caution:
The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.1
That said, this code can be used:
$row['condition'] = '$a == $b';
$a=10;
$b=20;
// Evaluate the condition using "return your_condition;"
if (eval( 'return ' . $row['condition'] . ';' ))
{
echo "true";
}
else
{
echo "false";
}
Test it here.
1http://php.net/manual/en/function.eval.php

Some Confusion in IF Condition of PHP

This is the code
$a = 'Rs 15.25';
if ( $a != '' && $a! = 0 ) {
echo "Inside If";
} else {
echo "Outside If";
}
actually I want to Print "Inside If" so that's why I put $a='Some String Value'. But it always prints "Outside If". Then I changed my code to
$a = 'Rs 15.25';
if ( $a != '' && $a != '0' ) {
echo "Inside If";
} else {
echo "Outside If";
}
I have just added single quotes to 0. Then i got the exact output as i want. But I didn't understand why this happens.
So please help me with this.
PHP does weak type comparison, that is, it converts both operands to the same type before doing the actual comparison.
If one of the operands is a number, the other one is converted to a number as well. If the second operand is a string and contains no digits, it is silently converted to the number 0.
To avoid this whole issue, use string type checking with the operator !== (=== for equality).
if($a !== '' && $a !== 0) {
echo "Inside If";
} else {
echo "Outside If";
}
First of all you when you have multiple conditions on an if statement you should always enclose each of them within brackets
So first thing you should do is to change your code to
$a='Rs 15.25';
if(($a!='') && ($a!='0'))
{
echo "Inside If";
}else
{
echo "Outside If";
}
In PHP 0 = FALSE, 1 = TRUE.
if($a != 0) -> if($a != FALSE)
if $a = 'Rs 15.25', $a != false and $a not empty, then you have echo "Outside If";

PHP if null echo else echo

Not sure what im doing wrong here, but the out come is always null. The script should output "you did not select an answer" only if no answer was selected but otherwise it should output the answer given:
I have updated the script as mentioned but still getting the empty output even when answer is given :/
Thanks for all the help so far guys, but even the below code doesnt work, it now just outputs as blank if no anwser, but if you do fill it in, it correctly echos the answer.
if (empty( $a1 )) {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p>You did not select an answer</p>\n"
. "</li>\n";
}
else {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p><strong>" . $q1[$a1] . ":</strong></p>\n"
. "<p>" . $r1[$a1] . "</p>\n"
. "</li>\n";
}
Completely forgot to show this part!!
// get local copies of single answers
$a1 = trim(isset($_POST['a1'])?$_POST['a1']:99);
$a3 = trim(isset($_POST['a3'])?$_POST['a3']:99);
$a4 = trim(isset($_POST['a4'])?$_POST['a4']:99);
$a5 = trim(isset($_POST['a5'])?$_POST['a5']:99);
Don't use if($a1 == null) use if(empty($a1)) or if(isset($a1))
An empty string is not null
$a1 = '';
if ($a1 == null) // is wrong
should be
$a1 = '';
if ($a1 === '')
or
if (empty($a1))
an empty is not the same as null try
if ($a === '') this respects also the type which is better for code quality
if (empty( $a1 )) {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p>You did not select an answer</p>\n"
. "</li>\n";
}
else {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p><strong>" . $q1[$a1] . ":</strong></p>\n"
. "<p>" . $r1[$a1] . "</p>\n"
. "</li>\n";
}
Use empty instead of null checking
'null' is not same as false or ''.'null' is an object.
In PHP, empty string ($a) & empty array ($b) will return true if you test following express:
$a = ''; $b = array();
$a == null -> TRUE $b == null -> TRUE
also,
$a == 0 -> TRUE
So you should use '===' to test, or there's always unexpected result in your code.

How to use strpos in PHP?

function check($text){
if(strpos('a', $text) == FALSE && strpos('b', $text) == FALSE){
echo 'error';
} else {
echo 'ok';
}
}
echo check('text') . "\n";
echo check('asd') . "\n";
echo check('bnm') . "\n";
echo check('test') . "\n";
echo check('abc') . "\n";
live: http://codepad.org/W025YYuH
why this not working? This return:
1 error 2 error 3 error 4 error 5 error
but should be:
1 error 2 ok 3 ok 4 error 5 ok
You should use === FALSE instead of == FALSE, as explained in the documentation
Additionally, your arguments are in the wrong order. Again, consult the documentation (or, as some people say, RTM)
Invert the position of the argument, first argument is the string, second argument is what do you search into the string.
strpos ( 'The string to search in' ,'the argument of search' )
Then == would not work as expected
because the position of 'a' was the 0th (first) character.
Try this:
function check($text){
if(strpos($text, 'a') === FALSE && strpos($text, 'b') === FALSE){
echo 'error';
} else {
echo 'ok';
}
}
echo check('text') . "\n";
echo check('asd') . "\n";
echo check('bnm') . "\n";
echo check('test') . "\n";
echo check('abc') . "\n";
You have the arfuments the wrong way around, change to:
if(strpos($text, 'a') === FALSE && strpos($text, 'b') === FALSE){
Also note that you need to check for a boolean false with the identical operator (===)

Wordpress QR Code Widget

Basically, I've been trying to make a simple Wordpress widget that displays a QR code with the URL of the current page. I'm using a modififed version of the simple text widget that parses PHP too.
function the_qrcode($permalink = '', $title = '') {
if($permalink && $title == '') {
$permalink = 'http://eternityofgamers.com/forums';
$title = 'Forums';
}
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=' .$permalink. '" alt="QR: ' .$title. '"/>;
}
Can someone tell me what's wrong with this? I get a 500 error when I add it to functions.php.
You will need to use the urlencode() function. Generally as a rule of thumb all querystring values should be url encoded.
function the_qrcode( $permalink = '' ) {
if($permalink == '') {
$permalink = 'http://eternityofgamers.com/forums';
}
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data='.urlencode($permalink);
}
Now you can create your QR code with:
the_qrcode(the_permalink());
Also, you had a very bad missing equals sign. It is very important to understand the difference between = and ==. If you don't, no matter the context = and == mean two different things. = assigns the right hand side to the left. == returns true or false whether the left and right hand side are loosely equal (loosely because casting will be used if the sides are not of the same type).
Look at this example (Codepad demo):
$a = 5;
$b = 10;
if($a = 6) {
echo "This always appears because when you assign a truthy (all non-zero numbers are true) to a variable, true is returned.\n";
echo "Also a should now equal six instead of five: " . $a . "\n";
}
if($b == 10) {
echo "This will work as expected because == is a comparison not an assignment.\n";
echo "And b should still be 10: " . $b;
}
Try with:
<?php
function the_permalink( $permalink ) {
if ($permalink == '') {
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data=http://eternityofgamers.com/forums" alt="QR Code">';
} else {
echo '<img src="http://api.qrserver.com/v1/create-qr-code/?data='.$permalink;
}
}
?>
(I've corrected a bunch of syntax errors)

Categories