This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 2 years ago.
I'm wondering if I could use the ternary operator for something like this:
var string = "";
if (something) {
string = "foo"
} else if (somethingElse) {
string = "bar";
} else if (bla) {
string = "pool";
} else if (xxxxx) {
string = "coffee";
} else {
string = "";
}
As far as I remember, I can do this in Java language:
String string = something?"foo":somethingElse?"bar":bla?"pool":xxxxx?"coffee":"";
But I'm not sure about PHP, I'm not even sure if it's OK to use ternary operator in this case or not.
E.g
if (something) {
string = "foo"
} else if (somethingElse) {
string = "bar";
} else if (bla) {
string = "pool";
} else if (xxxxx) {
string = "coffee";
} else {
string = "";
}
is equivalent to in PHP
(something) ? 'foo' : ((somethingElse) ? 'bar' : ((bla) ? 'pool' : ((xxxxx) ? 'coffe' : '')));
Related
This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
why the IF(the lastest one with else if and else) is doing all the time only first condition and only the first part ($filtry_1value[$key] = 'min_cena'), even if the condition shouldnt be true. I have another solution (less dynamic), if I will not fix this one, but I would like to know, why it is not working... I think it will be a trivial thing, but I cannot see it.
PS: I am working with laravel.
$filtry_1value = ['stat', 'lokalita', 'patro', 'min_cena', 'max_cena', 'min_uzitna_plocha', 'max_uzitna_plocha'];
foreach ($filtry_1value as $key => $filtr_1value) {
$filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
if(!empty($filtr_1value)){
if ($filtry_1value[$key] = 'min_cena' OR $filtry_1value[$key] = 'min_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
}
elseif ($filtry_1value[$key] = 'max_cena' OR $filtry_1value[$key] = 'max_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
}
else {
$query->where($filtry_1value[$key],'=', $filtr_1value);
}
}
}
may be-
foreach ($filtry_1value as $key => $filtr_1value) {
$filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
if(!empty($filtr_1value)){
if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
}
elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
}
else {
$query->where($filtry_1value[$key],'=', $filtr_1value);
}
}
}
You need to use the double equal sign for comparisons. == not a single =
Your if's should look like:-
if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
// ...
} elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
// ...
}
I'm fairly new to PHP so forgive me if this function is badly done.
I have a function:
function socialLink($sm_type = NULL) {
if ($sm_type = 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
In my code when I call the function socialLink('facebook'); it echo's the Twitter URL.
Surely it should echo the Facebook URL since $sm_type would be equal to 'facebook' not twitter ?
Any help would be appreciated.
Set your if condition with this,
function socialLink($sm_type = NULL) {
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
See this.
function socialLink($sm_type = NULL) {
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
}
NOTE: Single = use to assign the value and = = use to compare values
Different's Between = , = = , = = =
= operator Used to just assign the value.
= = operator Used to just compares the values not datatype
= = = operator Used to Compare the values as well as datatype.
Your if statement does not use a comparison operator, it is an assignment (=). For a comparison, please use "==".
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
if ($sm_type == 'twitter') {
echo 'https://www.twitter.com';
} else {
echo 'https://www.facebook.com';
}
In php == is use for string comparison so, In this case you can't used = for that, simple :)
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 7 years ago.
I have the following code:
<?php
$starting_id = 0;
$params = array ('val' => $starting_id);
echo parse_params ($params);
function parse_params ($params)
{
$query = ' WHERE ';
if ($params['val'] === NULL) {
$query .= ' IS NULL';
return $query;
}
if ($params['val'] == 'NOT NULL') {
$query .= ' IS NOT NULL';
return $query;
}
return $query.' = '.$params['val'];
}
When I run it, I expect to see this:
WHERE
instead, I get the following:
WHERE IS NOT NULL
Any ideas why?
According to the PHP type comparison tables,
$var = 0 ; // (int)
if you compare $var == "string",
it will return true, you need to type check for this
$var === "string"
check php type comparison
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
if ($form->isValid()) {
// ... perform some action, such as saving the task to the database
$nextAction = $form->get('saveAndAdd')->isClicked()
? 'task_new'
: 'task_success';
return $this->redirect($this->generateUrl($nextAction));
}
Here is the link to the documentation
http://symfony.com/doc/current/book/forms.html
The class documentation says that it returns a bool.
What is the point of
? 'task_new'
: 'task_sucess';
That is called "ternary" and it's awesome:
This is assigning the value $nextAction based on a condition. The first part (after the =) is the condition, like an if statement, the second part (after the ?) is the value assigned if the condition is true, and the last part (after the :) is the value assigned if the condition is false.
//the condition
$nextAction = $form->get('saveAndAdd')->isClicked()
? 'task_new' //true value
: 'task_success'; //false value
It is a shorter way of writing this:
if ($form->get('saveAndAdd')->isClicked()) {
$nextAction = 'task_new';
}
else {
$nextAction = 'task_success';
}
So, here's some easy examples:
$foo = (true) ? 'True value!' : 'False value!';
echo $foo; //'True value!' of course!
$foo = (false) ? 'True value!' : 'False value!';
echo $foo; //'False value!' of course!
It's the Ternary operator. The syntax is as follows:
value = (condition) ? run if true : run if false;
In this case, if $form->get('saveAndAdd')->isClicked() is true, then task_new. Else task_success.
If could be rewritten like so:
if($form->get('saveAndAdd')->isClicked()) {
$value = "task_new";
} else {
$value = "task_success";
}
The ternary operator is a shorter form for an if statement.
The : is the "else" part.
Example in Java:
boolean bool;
true ? bool = true : bool = false;
It's a senseless example, but shows the ternary operator very well.
if the condition, here true is "true", then fill into the variable bool true, else false.
alternative if-statement in Java to the code example above:
boolean bool;
if(true)
bool = true;
else
bool = false;
This is a Ternary Operator which is a short hand if else statement. This is equivalent to
if($form->get('saveAndAdd')->isClicked()){
$nextAction = 'task_new'
else{
$nextAction = 'tassk_success'
}
This is the ternary opeator, a short-hand expression that works the same as an if
$value = someFunc() ? "whatever" : "the other"
is equivalent to
if (someFunc()) {
$value = "whatever";
} else {
$value = "the other";
}
This is equivalent to "if" and "else" statements.
This code :
$nextAction = $form->get('saveAndAdd')->isClicked()
? 'task_new'
: 'task_success';
is equivalent to this code :
if ( $form->get('saveAndAdd')->isClicked() )
{
$nextAction = 'task_new';
}
else
{
$nextAction = 'task_success';
}
This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 2 years ago.
I have this piece of code that is unclear to me, specifically the complex use of the ternary
operators
if (!$byField && is_numeric($v)){ // by ID
$r=$fromRow?
$fromRow:
($v?
dbRow("select * from pages where id=$v limit 1"):
array()
);
}
if someone could explain how to evaluate the nested use of ternary operators
Using nested ternary operators in your code adds unnecessary complexity. For the same reason, it should not be used. Just use a normal if-else block instead. That's far more readable.
if (condition) {
# code...
}
else {
# code...
}
To answer your question:
$r = $fromRow ? $fromRow : ( $v ? dbRow("..."): array() );
The above statement can be rewrote as follows:
if (!$byField && is_numeric($v))
{
if ($fromRow)
{
$r = $fromRow;
}
elseif ($v)
{
$r = dbRow("select * from pages where id=$v limit 1"):
}
else
{
$r = array();
}
}
As you can see, it's more readable.
Consider the following code:
<?php
$a = true;
$b = false;
$c = true;
echo (
$a
? 'A is true'
: (
$b
? 'A is false, but B is true'
: (
$c
? 'A is false, B is false, but C is true'
: 'A, B and C are all false'
)
)
);
?>
Which could easily be rewritten as so:
<?php
if ($a) {
echo 'A is true';
} else {
if ($b) {
echo 'A is false, but B is true';
} else {
if ($c) {
echo 'A is false, B is false but C is true';
} else {
echo 'A, B and C are all false';
}
}
}
?>
if (!$byField && is_numeric($v)){ // by ID
if ($fromRow) {
$r = $fromRow;
else if ($v) {
$r = dbRow("select * from pages where id=$v limit 1"):
} else {
$r = array();
}
}