php operator

How can I make the below return false. Is there an alternative for the greater operator (>), like strict greater?

if ('2test'>0) echo 'true';  
else echo 'false';
LVL 1
myyisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Comparison operators are documented on the PHP.net web site.  
http://php.net/manual/en/language.operators.comparison.php
http://php.net/manual/en/types.comparisons.php

Because PHP is a loosely typed language, it can "type juggle" the data when making loose comparisons.
http://php.net/manual/en/language.types.type-juggling.php

There is a "less than" sign < that will give you a false indicator in the expression.
0
Marco GasiFreelancerCommented:
'2test' is a string, so if you write

if ('2test'>0) echo 'true';  

Open in new window


Php will get the length of the string '2test' and it will see that is greater than 0 so the condition always will return true. Maybe you want to wrrite $2test without quotes?

In addition if have to write

if ('2test'>0) {
   echo 'true';  
} else {
  echo 'false'; 
}

Open in new window

0
myyisAuthor Commented:
If the first character is numeric than interprets as integer,
if not it is string. There should be something to avoid the "first character check".


 ('test'>0)  -->  returns  false
 (' 2test'>0)  -->  returns  true
 ('_2test'>0)  -->  returns  false
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Chris StanyonWebDevCommented:
What logic are you trying to apply here? How do want your code to behave - asking if some text is greater than some number doesn't really make logical sense?

Are you wanting the comparison to work on string length, just the numbers from the string, the first number, the first character?
0
myyisAuthor Commented:
$var1=1;
$var2='1test';

if ($var1>0)  echo 'true';

I want to make the below return false using the ">0" criteria since it is a string.

if ($var2>0)  echo 'true'; else return 'false';
0
Chris StanyonWebDevCommented:
Then check to make sure it's numeric before checking whether it's greater than 0

if (is_numeric($var1) && $var1>0) {
   echo 'true';
} else {
   echo 'false';
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
There should be something to avoid the "first character check".
Ha!  There should be pink unicorns, too.  But this is just the way PHP works.  I cannot think of any computer-science application where it would make sense to compare a multi-byte string variable to an integer.  Programmers just don't do that sort of thing.  If we have a little "higher level" view of what you're trying to achieve we might be able to suggest a better way to make sense of it.

<?php // RAY_temp_myyis.php
error_reporting(E_ALL);

$var1=1;
$var2='1test';

if ($var1>0)  echo 'true';

// I want to make the below return false using the ">0" criteria since it is a string.

if ($var2>0)  echo 'false';

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.