0
<?php

function some_func(){

 return 'some_str_type' && 'another_str_type';
}

function another_func(){

 return '123' || '456';
}


print some_func(); //1 - the same as true

print another_func(); //again prints 1, as true

The clean style of coding of any language dictates, to drop non-small function into small ones - because one function SHOULD return single value.

BUT, i saw this approach in source of some popular php-template langs (smarty, dwoo). So what is that? When to code this way? (mean any real-world situation)

1 Answer 1

4

PHP will return 1 value. What you are doing above is you type an expression, that is evaluated, and the resulting boolean is returned.

return 'some_str_type' && 'another_str_type';

becomes

return true && true;

becomes

return true;

When to use in real life:

function some_func(){
   $success1 = doStuff1();
   $success2 = dostuff2();
   return $success1 && $success2;
}

it will return true if both called functions return true.

Sign up to request clarification or add additional context in comments.

2 Comments

And last example with or would return true if at least one is true.
The expression is indeed parsed as you would expect from a logical expression :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.