PHP - And/Or Keywords

PHP - and / or keywords

and and or have higher lower precedence than && and ||. To be more exact && and || have higher precedence than assignment operator ( = ) while and and or have lower.

http://www.php.net/manual/en/language.operators.precedence.php

Usually it doesn't make a difference, but there are cases when not knowing about this difference can cause some unexpected behaviour. See examples here:

http://php.net/manual/en/language.operators.logical.php

Alternative to the 'or' keyword in PHP

The statement with "or" works, because the PHP-Interpreter is quite intelligent:
Because a connection of "or"'s is true, if the first of them is true, it stops executing the statement when the first one is true.

Imagine following (PHP)code:

function _true()  { echo "_true";  return true;  }
function _false() { echo "_false"; return false; }

now you can chain the function calls and see in the output what happens:

_true() or _true() or _true();

will tell you only "_true", because the chain is ended after the first one was true, the other two will never be executed.

_false() or _true() or _true();

will give "_false_true" because the first function returns false and the interpreter goes on.

The same works with "and"

You can also do the same with "and", with the difference that a chain of "and"'s is finished, when the first "false" occurres:

_false() and _true() and _true();

will echo "_false" because there the result is already finished an cannot be changed anymore.

_true() and _true() and _false();

will write "_true_true_false".

Because most of all functions indicate their success by returning "1" on success and 0on error you can do stuff like function() or die().
But some functions (in php quite seldom) return "0" on succes, and != 0 to indicate a specific error. Then you may have to use function() and die().

PHP with keyword - what does with do?

with is not a keyword, it's a laravel function. The extra space between with and ( is a red herring.

The 5.2 docs include it in miscellaneous helpers. The source is available on github as well

How do the And and Or keywords behave in ASP when comparing numbers?

Unfortunately there isn't an easy way to convert this without spending time. There is a neat tool you can use here... but you'll have to read the comments in the converted code as it will tell you there are assumptions -- such as user functions that aren't defined.. from there you'll need to write your own or try to figure it out.

But, to directly answer your question, those are bitwise operations and can be done such as Byte2 & 240

Keyword matching in an array/string of keywords using PHP

I've managed to achieve result:

William Shakespeare 10 artist 1 Artist 1 Frank Sinatra 8 Frank Sinatra
8

using the code:

<?php

mb_internal_encoding('UTF-8');
$words = array(
'Artist' => '1',
'Tony' => '2',
'Scarface' => '3',
'Omar' => '4',
'Frank' => '5',
'Torrentino' => '6',
'Mel Gibson' => '7',
'Frank Sinatra' => '8',
'Shakes' => '9',
'William Shakespeare' => '10'
);


uksort($words, function ($a, $b) {
$as = mb_strlen($a);
$bs = mb_strlen($b);

if ($as > $bs) {
return -1;
}
else if ($bs > $as) {
return 1;
}
return 0;


});

$words_ci = array();

foreach ($words as $k => $v) {
$words_ci[mb_strtolower($k)] = $v;
}

$text = "William Shakespeare is very famous in the world. An artist is a person engaged in one or more of any of a broad spectrum of activities related to creating art, practicing the arts, and/or demonstrating an art. Artist is a descriptive term applied to a person who engages in an activity deemed to be an art. Frank Sinatra was an American singer, actor, director, film producer, and conductor. Frank Sinatra was born on December 12, 1915, in Hoboken, New Jersey, the only child of Italian immigrants Natalina Garaventa and Antonino Martino Sinatra, and was raised Roman Catholic.";

$re = '/\b(?:' . join('|', array_map(function($keyword) {
return preg_quote($keyword, '/');
}, array_keys($words))) . ')\b/i';



preg_match_all($re, $text, $matches);
foreach ($matches[0] as $keyword) {
echo $keyword, " ", $words_ci[mb_strtolower($keyword)], "\n";
}

&& and 'and' are the same in php?

They do not have the same precedence. Fully parenthesised, your code is:

($keyWord = 2) and (3>4); // $keyWord = 2
$symbol = (2 && (3>4)); // $symbol = false

2 and false are clearly not the same, hence 'not-equal'.

More on operator precedence

Search for more than one keyword

If you're searching by the full name you'll need to add a condition for it

CONCAT(`first_name`,' ',`last_name`) LIKE '%{$keywords}%' OR


Related Topics



Leave a reply



Submit