eval error when using "if" short form - php

when doing something like
$date = mktime();
$xxx = 'if ( date("N",$date ) == 1 ) { return TRUE; } else { return FALSE; }';
$yyy = eval( $xxx );
echo $yyy;
it works.
But when doing something like
$date = mktime();
$xxx = '( date("N",$date) == 1 ? return TRUE : return FALSE );';
$yyy = eval( $xxx );
echo $yyy;
I get an error like
Parse error: syntax error, unexpected T_RETURN in /my_path/my_file.php(107) : eval()'d code on line 1
Why ?

This has nothing at all to do with eval.
Let's create the real testcase:
<?php
function foo()
{
$date = mktime();
( date("N",$date) == 1 ? return TRUE : return FALSE );
}
foo();
?>
Output:
Parse error: syntax error, unexpected T_RETURN on line 5
return is a statement, not an expression, so you can't nest it into an expression which is what you're trying to do here. The conditional operator is not a one-line replacement for if/else.
To use the conditional operator properly:
return (date("N",$date) == 1 ? TRUE : FALSE);
which simplifies to just:
return (date("N",$date) == 1);
In your code, that looks like this:
$date = mktime();
$xxx = 'return (date("N",$date) == 1);';
$yyy = eval($xxx);
echo $yyy;

I'm pretty certain that should be
$xxx = 'return ( date("N",$date) == 1 ? TRUE : FALSE );';
The things generated by a ternary operator are values (expressions) rather than commands.

Related

Using an if statement in a variable in php

I am trying to use an if statement in a variable in wordpress such that if the page-id is 17711 the variable should be equal to 4 if not true is should be equal to intval( get_option('wp_estate_prop_no', '') );
The code i am trying to use is:
$prop_no = if (is_page(17711)) { 4}
else {
intval( get_option('wp_estate_prop_no', '') );
}
I'm unsure what your is_page function does, but you should be able to use PHP's ternary operator to achieve an identical effect.
$prop_no = (is_page(17711) ? 4 : intval( get_option('wp_estate_prop_no', '')));
Try this:
$prop_no = (is_page(17711)?4:intval(get_option('wp_estate_prop_no,''));
Read about here : https://davidwalsh.name/php-ternary-examples
yo can use $proper_no = is_page(17711) ? 4 : intval( get_option('wp_estate_prop_no', ''));
or.
if(is_page(17711) == 4){
$proper_no = 4;
}else{
$proper_no = get_option('wp_estate_prop_no', '');
}

What does ':' and '?' mean in the isClicked() symfony [duplicate]

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';
}

understanding nested ternary operators [duplicate]

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();
}
}

PEG-php parsing if statement

Good day.
Just started to use PEG.
I'm familiar with wp:peg
and some other theory, but still can't understand how it internally works.
My task is to parse expressions like
if($a=='qwerty' && $b>2 || $c<=5)
print 'something';
endif;
Body of is statement contains only print operator and nothing else, but it has complex condition.
My thinking about it:
/*!* Calculator
Int: /[0-9]+/
Var: /\$[a-z]+/
tThen: /then{1}/
tIf: /if{1}/
tElse: /else{1}/
tEndif: /endif{1}/
block: /.+/
condEq: Var '==' ( Int | Var ) *
condStatement: '(' condEq ')'
function condEq( &$result, $sub ) {
if( eval($sub['text'].';') ) {
$result['text'] = 'true';
}
else {
$result['text'] = 'false';
}
}
ifStatement: tIf condStatement
Expr: ifStatement
*/
But I believe this task has better solution:)
Can you help me with it?
Thanks.

can php return a boolean? => return $aantal == 0;

can php return a boolean like this:
return $aantal == 0;
like in java you can
public boolean test(int i)
{
return i==0;
}
or do you Have to use a if contruction?
because if i do this.
$foutLoos = checkFoutloos($aantal);
function checkFoutloos($aantal)
{
return $aantal == 0;
}
echo "foutLoos = $foutLoos";
it echo's
foutLoos =
so not true or false
thanks
matthy
It returns a boolean, but the boolean is not converted to a string when you output it. Try this instead:
$foutLoos = checkFoutloos($aantal);
function checkFoutloos($aantal)
{
return $aantal == 0;
}
echo "foutLoos = " . ( $foutLoos ? "true" : "false" );
Try it out!
function is_zero($n) {
return $n == 0;
}
echo gettype(is_zero(0));
The output:
boolean
yes ideed i found out that when you echo a false you get nothing and true echo's 1 thats why i was confused ;)
Yes. you can even through in the ternary operator.
function foo($bar == 0) {
return ($bar) ? true : false;
}
Yes, you can return a boolean test in the return function. I like to put mine in parenthesis so I know what is being evaluated.
function Foo($Bar= 0) {
return ($Bar == 0);
}
$Return = Foo(2);
$Type = var_export($Return, true);
echo "Return Type: ".$Type; // Return Type: boolean(true)
In fact, almost anything can be evaluated on the return line. Don't go crazy though, as it may make refactoring more difficult (if you want to allow plugins to manipulate the return, for instance).
You could just do it like this i think, with type casting:
return (bool) $aantal;

Categories