Using php how to match boolean value with word in sentence? - php

I have a response from aws like this with boolean value :
$string=/vasff/fdsfsdf:boolean true
and $string=/sadasff/fdsfsdf:boolean false
in their documentation, they had written a response in boolean.// Success? (Boolean, not a CFResponse object).
Their documentaion is https://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_doesObjectExist
how to match value is true or false? I tried this code but it always returns true.
if(stripos($string, true) !== false){
echo "true";
}
else{
echo "false";
}
EDIT
Just wanted to thank everyone for their good answers on this.
This code is working fine for aws s3 bucket response.
if($response == FALSE){
return false; }else{
return true;
}

if you mean by this :
$string=/vasff/fdsfsdf:boolean true
this :
$string='/vasff/fdsfsdf:boolean true';
you can use this code : use true with double quote
$string='/vasff/fdsfsdf:boolean true';
if(stripos($string, "true") !== false){
echo "true";
}
else{
echo "false";
}
Edit :
use your function paramaters intead of ....
if(doesObjectExist(....)){
echo "true";
}else{
echo "false";
}

You can try this:
preg_match_all("/:boolean (.+)/", $string, $mc);
if(isset($mc[1]) && isset($mc[1][0])){
if($mc[1][0] == "false"){
echo $mc[1][0] . " == false";
}
else{
echo $mc[1][0] . " == true";
}
}

Related

Check for multiple null isset values

I am checking if two values are null. If both are null I want to return false, if either or both are not null, I want to return true.
My current code returns true only when both are not null but I want it to return true when either or not null.
// check if both null
if (!isset($myarray['dataone'], $myarray['datatwo']))
{
echo 'false';
);
} else {
echo 'true';
);
}
return $emptytabs;
For that you can use relational operators. AND (&&) OR (||)
By using AND (&&) operators.
if ( (!isset($myarray['dataone']) || (!isset$myarray['datatwo'] ))
{
echo 'false';
}
else
{
echo 'true';
}
By using OR ( || ) operators.
if (isset($myarray['dataone'] && isset$myarray['datatwo'])
{
echo 'false';
}
else
{
echo 'true';
}
// check if both null
if ( !isset($myarray['dataone']) && !isset($myarray['datatwo'])) {
echo 'false';
} else {
echo 'true';
}
// check if one or both are null
if ( !isset($myarray['dataone']) || !isset($myarray['datatwo'])) {
echo 'false';
} else {
echo 'true';
}
// check if both null
if ( !isset($myarray['dataone'], $myarray['datatwo']) )
{
echo 'false';
} else {
echo 'true';
}
return $emptytabs;
this approach you provided is totally true , but it only return true if all the provided parameters are set according to php documentations .
so your code should works correctly . except you have unwanted parentheses that should deleted
The simplest way is to use the OR (||) operator. You want to show 'true' if one thing is set OR another thing is set. Just say that with code.
if ( isset($myarray['dataone']) || isset($myarray['datatwo']) ) {
echo 'true';
} else {
echo 'false';
}
Using the AND operator adds pointless complexity by checking that both of the two things are not set in order for it to show 'false'. That's not an intuitive way to think about it, so it doesn't make sense to write the code that way.
DISCLAIMER: This answer is opinionated.

How to treat zero values as true using php?

Here is my sample code:
$issue_id = $_POST['issue_id'];
if(!empty($issue_id)){
echo 'true';
}
else{
echo 'false';
}
If I pass 0 to $_POST['issue_id'] by form submitting then it echo false. Which I want is: Condition will be true if the following conditions are fulfilled:
1. true when I pass any value having 0.
2. false when I don't pass any value. i.e: $_POST['issue_id'] is undefined.
I also tried this:
if(!isset($issue_id)){
echo 'true';
}
else{
echo 'false';
}
if(!empty($issue_id) || $issue==0){
echo 'true';
}
else{
echo 'false';
}
The last one is okay, meaning if I pass any value having ZERO then it will echo true. But it will also echo true if I don't pass any value. Any idea?
The last is okay, meaning if I pass any value having ZERO then it echo true. But it also echo true if I don't pass any value. Any idea?
if (isset($_POST["issue_id"]) && $_POST["issue_id"] !== "") {
}
please notice I used !== not !=. this is why:
0 == "" // true
0 === "" // false
See more at http://php.net/manual/en/language.operators.comparison.php
also if you are expecting number you can use
if (isset($_POST["issue_id"]) && is_numeric($_POST["issue_id"])) {
}
since is_numeric("") returns false
http://php.net/manual/en/function.is-numeric.php
Alternatively if you expect number good option is filter_var
if (isset($_POST["issue_id"]) {
$issue_id = filter_var($_POST["issue_id"], FILTER_VALIDATE_INT);
if ($issue_id !== false) {
}
}
since filter_var("", FILTER_VALIDATE_INT) will returns false and filter_var("0", FILTER_VALIDATE_INT) will return (int) 0
http://php.net/manual/en/function.filter-var.php
if(isset($_POST['issue_id'])) {
if($_POST['issue_id'] == 0) {
echo "true";
}
else {
echo "false";
}
}
When you get data from a form, remember:
All text boxes, whether input or textarea will come as strings. That includes empty text boxes, and text boxes which contain numbers.
All selected buttons will have a value, but buttons which are not selected will not be present at all. This includes radio buttons, check boxes and actual buttons.
This means that $_POST['issue_id'] will be the string '0', which is actually truthy.
If you need it to be an integer, use something like: $issue_id=intval($_POST['issue_id']);
#Abdus Sattar Bhuiyan you can also full fill your two condition like below one:
<?php
$_POST["issue_id"] = "0";
$issue_id = isset($_POST['issue_id']) ? (!empty($_POST['issue_id']) || $_POST['issue_id'] === 0 || $_POST['issue_id'] === "0") ? true : false : false;
if($issue_id){
echo 'true';
}
else{
echo 'false';
}

How does PHP SimpleXML get evaluated as an expression?

Consider this PHP code:
<?php
$xmlString =
'<'.'?xml version="1.0" encoding="UTF-8"?'.'>' .
'<rootEl>' .
'<a attrA="valA">xxx</a>' .
'<b attrB="valB"/>' .
'<c>oink</c>' .
'<d/>'.
'<e>' .
'<f>zzz</f>' .
'</e>' .
'</rootEl>';
$xml = simplexml_load_string( $xmlString );
foreach ( $xml->children() as $child ) {
echo "\$CHILD [" . $child->getName() . "]: " . $child->asXML();
echo "\n";
$insideIf1 = false;
if ($child) { $insideIf1 = true; }
$insideIf2 = false;
if (true == $child) { $insideIf2 = true; }
$insideIf3 = false;
if ((boolean)$child) { $insideIf3 = true; }
echo " - if(\$CHILD) return \"true\"; else return \"false\"; : " . (($insideIf1)?"true":"false")."\n";
echo " - if(true == \$CHILD) return \"true\"; else return \"false\"; : " . (($insideIf2)?"true":"false")."\n";
echo " - if((boolean)\$CHILD) return \"true\"; else return \"false\"; : " . (($insideIf3)?"true":"false")."\n";
echo " - ((\$CHILD)?\"true\":\"false\"): " . (($child)?"true":"false")."\n";
echo " - ((true == \$CHILD)?\"true\":\"false\"): " . ((true == $child)?"true":"false")."\n";
echo " - (((boolean)\$CHILD)?\"true\":\"false\"): " . (((boolean)$child)?"true":"false")."\n";
echo "\n";
}
As per PHP documentation (online, see http://php.net/manual/en/control-structures.if.php):
As described in the section about expressions, expression is evaluated
to its Boolean value. If expression evaluates to TRUE, PHP will
execute statement, and if it evaluates to FALSE - it'll ignore it.
More information about what values evaluate to FALSE can be found in
the 'Converting to boolean' section.
The "converting to boolean" section of this explanation states:
Converting to boolean
When converting to boolean, the following values are considered FALSE:
...omissis...
SimpleXML objects created from empty tags
If you execute the code above, these are the results on my PHP 5.5.9-1ubuntu4.5 (cli):
$CHILD [a]: <a attrA="valA">xxx</a>
- if($CHILD) return "true"; else return "false"; : true
- if(true == $CHILD) return "true"; else return "false"; : true
- if((boolean)$CHILD) return "true"; else return "false"; : true
- (($CHILD)?"true":"false"): true
- ((true == $CHILD)?"true":"false"): true
- (((boolean)$CHILD)?"true":"false"): true
$CHILD [b]: <b attrB="valB"/>
- if($CHILD) return "true"; else return "false"; : true
- if(true == $CHILD) return "true"; else return "false"; : false
- if((boolean)$CHILD) return "true"; else return "false"; : true
- (($CHILD)?"true":"false"): true
- ((true == $CHILD)?"true":"false"): false
- (((boolean)$CHILD)?"true":"false"): true
$CHILD [c]: <c>oink</c>
- if($CHILD) return "true"; else return "false"; : false
- if(true == $CHILD) return "true"; else return "false"; : true
- if((boolean)$CHILD) return "true"; else return "false"; : false
- (($CHILD)?"true":"false"): false
- ((true == $CHILD)?"true":"false"): true
- (((boolean)$CHILD)?"true":"false"): false
$CHILD [d]: <d/>
- if($CHILD) return "true"; else return "false"; : false
- if(true == $CHILD) return "true"; else return "false"; : false
- if((boolean)$CHILD) return "true"; else return "false"; : false
- (($CHILD)?"true":"false"): false
- ((true == $CHILD)?"true":"false"): false
- (((boolean)$CHILD)?"true":"false"): false
$CHILD [e]: <e><f>zzz</f></e>
- if($CHILD) return "true"; else return "false"; : true
- if(true == $CHILD) return "true"; else return "false"; : false
- if((boolean)$CHILD) return "true"; else return "false"; : true
- (($CHILD)?"true":"false"): true
- ((true == $CHILD)?"true":"false"): false
- (((boolean)$CHILD)?"true":"false"): true
Children elements "b" and "d" ARE empty, therefore I was expecting "false", and "a", "c", "e" ARE NOT empty, so I was expecting "true".
As is comes out, that statement in PHP documentation is absolutely NOT true, and the behaviour doesn't even seem consistent: implicit or explicit casts to boolean don't behave the same way (see case of element "c"), and if the bare SimpleXML object is used as "if" conditional expression (both "if" and ternary operator), somehow that expression evaluates differently from a cast to boolean.
Does anyone have an idea of what's going on here?
First of all, let's discard some of your code.
foreach ( $xml->children() as $child ) {
echo "\$CHILD [" . htmlspecialchars($child->getName()) . "]: " . htmlspecialchars($child->asXML());
echo "\n";
$insideIf1 = false;
if ($child) { $insideIf1 = true; }
$insideIf2 = false;
if (true == $child) { $insideIf2 = true; }
$insideIf3 = false;
if ((boolean)$child) { $insideIf3 = true; }
var_dump($child);
// echo intval($child)."\n";
echo 'VALUE EVALUATES TO: '. $child."\n";
echo " - if(\$CHILD) return \"true\"; else return \"false\"; : " . (($insideIf1)?"true":"false")."\n";
echo " - if(true == \$CHILD) return \"true\"; else return \"false\"; : " . (($insideIf2)?"true":"false")."\n";
echo " - if((boolean)\$CHILD) return \"true\"; else return \"false\"; : " . (($insideIf3)?"true":"false")."\n";
// This part evaluated exactly same as above. So no need to make confusion
echo "\n";
}
Next:
First and third test
Those are basically same. Object is considered true, if it has properties. So basically xml tag without attributes has no properties (just var_dump them).
Objects without properties (does not have attributes BUT NOTE THEY CAN have value - which is returned by iterator) considered false: c, d
Second test
This test on the other hand does not test object itself. But all values returned by it from casting to: int, bool and string.
One of those tests calls for __toString magic method. Which returns value. Then it evaluates to true (Elements a and c)
So its not random, but has special rules while considering it true or false.
Forgot to add
Note: SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump() or anything else which can examine objects.
This "strange" behaviour is result of it implementing inerators.
Edit:
As you stated in comment what made you analyze this behaviour. I think that you should never chose to check SimpleXML objects on if($obj) basis.
Instead, use SimpleXML::attributes() or strval to do all checks. Then you're sure what you're checking.
Edit2:
$xmlObject is considered true (is not empty) if it has Properties OR Chilren (subnodes) (that works fine)
To check if object has value you cast it to string.
In this case, empty does not mean if it has value, only if it has children or properties.

Variable set to false

I wrote this REALLY simple code:
$foo=false;
echo $foo;//It outputs nothing
Why? Shouldn't it output false? What can I do to make that work?
false evaluates to an empty string when printing to the page.
Use
echo $foo ? "true" : "false";
The string "false" is not equal to false. When you convert false to a string, you get an empty string.
What you have is implicitly doing this: echo (string) $foo;
If you want to see a "true" or "false" string when you echo for tests etc you could always use a simple function like this:
// Boolean to string function
function booleanToString($bool){
if (is_bool($bool) === true) {
if($bool == true){
return "true";
} else {
return "false";
}
} else {
return NULL;
}
}
Then to use it:
// Setup some boolean variables
$Var_Bool_01 = true;
$Var_Bool_02 = false;
// Echo the results using the function
echo "Boolean 01 = " . booleanToString($Var_Bool_01) . "<br />"; // true
echo "Boolean 02 = " . booleanToString($Var_Bool_02) . "<br />"; // false

Check if page returns true or false

I am trying to verify a Minecraft username is paid or not.
By typing in the username at the end of the URL, it returns true or false.
$input = 'Notch';
function checkPlayer($player) {
$mcURL = 'http://www.minecraft.net/haspaid.jsp?user=';
$auth = file_get_contents($mcURL . $player);
if ($auth === true) {
echo $player. ' is valid';
} else {
echo $player. ' is not valid';
}
}
checkPlayer($input);
But it doesn't return true. By going to the page http://www.minecraft.net/haspaid.jsp?user=Notch, it does return true. How do I properly check? I think file_get_contents is the wrong function to use for this matter. I'm not sure though.
change this line :
if ($auth === true) {
with
if (trim($auth) == "true") {

Categories