Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How it will print hello - If hello is true, then the function must print hello, but if the function doesn’t receive hello or hello is false the function must print bye.
<?php
function showMessage($hello=false){
echo ($hello)?'hello':'bye';
}
?>
So if you don't want any condition you can add default value bye to para,eter. And simply echo it
<?php
function showMessage($hello="bye"){
echo $hello;
}
?>
Basically ($hello)?'hello':'bye'; is the shorthand for:
if ($hello == true) {
echo 'hello';
} else {
echo 'bye';
}
Reference: http://php.net/manual/en/control-structures.if.php
You are using ternary operator inside function, which will check the type of variable true or false. By default $hello variable type will be false.
So below code will be check if variable type is true then prine 'hello' else ternary operator will be print 'bye'.
It is same as like below
if($hello==true){
echo 'hello';
}else{
echo 'bye';
}
The reason why showMessage('abc') now prints 'hello' is because the ($hello) will evaluate to true as a non-empty string.
I guess what you are looking for is the type comparison operator ===. It will check whether the argument passed is actually a boolean value.
function showMessage($hello=false) {
echo ($hello === true)?'hello':'bye';
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to make a logic to check multiple conditions without using if else condition numbers of time.
I have 4 variables and i have to check whether it is empty or not.
if one of the variable is blank than want to do something.
I have function like this..
function searchResult($genre, $subject, $type, $grade){
// checking conditions here
}
please suggest me simple method.
You can use func_get_args, array_filter and func_num_args as well:
function searchResult($genre, $subject, $type, $grade){
if (count(array_filter(func_get_args())) < func_num_args()) {
echo 'One or many arguments are empty.. do something';
}
}
Pay attention to the array_filter function:
If no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed.
function searchResult($genre, $subject, $type, $grade){
if((!empty($genre)) && (!empty($subject)) && (!empty($type)) )
{
echo "not empty";
}
else
{
echo "empty";
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So i have this code:
echo $argi[2][$iterate2][$iterate];
if ($argi[2][$iterate2][$iterate]==TRUE){
echo " It's true ";
}
And the result is:
true It's true
or
false It's true
Do you know why?
I tried to change IF to var=TRUE, var===TRUE, var=="true", var=1
None of these worked. I haven't got that kind of problem earlier, it's really weird...
P.S. If statement is inside two foreach loops. I don't know if that matters somehow...
EDIT
I changed echo to var_dump, and the result is:
string(5) "false" It's true
EDIT 2
Here's the part of the script:
$iterate2=0;
foreach ($UpdateData as $key => $value) {
$sql.="UPDATE `addtmptable` SET ";
$sql.="$key = CASE ";
$iterate=0;
foreach ($value as $val) {
$sql.="WHEN user = '$UpdateDataU[$iterate]' THEN $val ";
var_dump($argi[2][$iterate2][$iterate]);
if ($argi[2][$iterate2][$iterate]==true){
echo " It's true ";
}
$iterate++;
}
$sql.=";";
$iterate2++;
}
I stopped on this, i want to fire the inner foreach if the statement is true
You are testing a string against a boolean value.
The only strings that evaluate to false are "" and "0".
if (strtolower($argi[2][$iterate2][$iterate]) != "false") {
echo " It's true ";
}
A string with any value will always evaluate to boolean value true. Change your comparison to $argi[2][$iterate2][$iterate] == "TRUE" instead.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have an array like this
$cars=array("Volvo","BMW","Toyota");
and I use this function to verify if the car BMW is in the array
<?php
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);
for($x=0;$x<$arrlength;$x++) {
echo $cars[$x];
echo "<br>";
if($cars[$x]="Mercedes"){
echo "OK ";
}
else
{
echo "NO ";
}
}
?>
But the result is:
Volvo
OK BMW
OK Toyota
OK
How the result is this?
Your problem is down to this line:
if($cars[$x]="Mercedes")
You are not comparing but assigning instead. You need to use a double-equal for compmarison: == - for your purpose.
However a much better solution would be to use in_array function:
if(in_array("Mercedes", $cars)) {
echo "OK";
}
Use [in_array()][1] to check whether a value is found in an array.
I would use the in_array() function http://php.net/manual/en/function.in-array.php
The direct answer to your question is because = and == are not the same. It should be if( $cars[$x] == "Mercedes")
However you can avoid the problem entirely by using the appropriate built-in functions:
if( in_array("Mercedes",$cars)) echo "Oh look a fancy car!";
I would suggest you this function:
http://www.w3schools.com/php/func_array_in_array.asp
Use in_array() to check whether a value is found in an array. Like so:
$inArray = in_array("BMW", $cars);
if ($inArray)
echo 'OK';
Your main problem is that you're not using the comparison operator ==, but instead, you are using = which is the operator used to assign variables, so if($cars[$x]="Mercedes") is always evaluating to true and therefore you're seeing OK everytime. So you have to use == instead
On the other hand, what you want may be accomplished in a better way, using in_array()
$cars=array("Volvo","BMW","Toyota");
if(in_array('Mercedes', $cars)) echo 'OK';
However, if you're also interested in finding out how many Mercedes' are in there, you need to do the following
$cars=array("Volvo","BMW","Toyota");
$cars = array_count_values($cars);
if(isset($cars['Mercedes'])) {
echo 'There are '.$cars['Mercedes'].' mercedes in the array';
} else {
echo 'There are no mercedes in the array.';
}
First, you are looking to every elements in your array:
for each element:
you display the name with echo $cars[$x];
return to a new line with <br>
if it is a "Mercedes" you display "OK"; if it's not "NO"
BUT in fact you don't test if it is a "Mercedes" because you doesn't use a comparator sign (==) but an assignement sign (=) so it is always true.
If you only want to check if a "Mercedes" is in the array use the php function 'in_array' (SEE DOC)
<?php
$cars=array("Volvo","BMW","Toyota");
if(in_array("Mercedes",$cars)) {
echo "OK ";
}
else
{
echo "NO ";
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Im wondering if i can do and/or together. I'm trying to make a code that checks if both variables are wrong or if only one of them is wrong and the other one is right then I want to show a message like: "one of the "variables" are wrong.
Can anyone help me out here?
In addition to Tim Whites answer, if you don't want it nested, you could also use:
if($a == false AND $b == false) { echo "Both variables are false"; }
elseif($a == false OR $b == false) { echo "One variable is false"; }
if ($a==false OR $b==false) {
if($a==false AND $b==false) { echo "Both variables are false"; }
else { echo "On of the variables is false"; }
}
Something like that will do.
You could just use multiple IF statements, to check if 1 variable is wrong or both are wrong. Like:
if ($variable1==false && $variable2==false){
}
else{
if ($variable1==false || $variable2==false){
}
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
In the javascript world we can run a function through a Ternary comparison, I wanted to see if this applies to PHP, it seems it does only to an extent.
This is not for production use nor will it be actually used. This is merely a topic to see PHP's in-depth extent of the ternary comparison.
<?
$a = 1;
$b = 1;
function doThis($a){
print "$a";
}
$a == $b ? ( doThis('TRUE') ):( print "FALSE" );
?>
The above code works perfectly, however, is it possible to run multiple functions and or operations within ()?
Such as?
$a == $b ? ( doThis('TRUE'), doThis('THAT') ):( print "FALSE" );
or even?
$a == $b ? ( function(){ print "33"; doThis("TRUE") } ):( print "FALSE" );
You can have the ternary return a closure that would perform the requested function
$func = $a==$b?function(){ print "33"; doThis("TRUE"); }:function(){ print "FALSE"}); );
$func();
or borrowing from javascript you can create a IIFE (Immediately Invoked Function Expression)
$a==$b?call_user_func(function(){print "33"; doThis("TRUE");}):
call_user_func(function(){print "FALSE"; });