<?php
var_dump(preg_match('#((.*)*/)#i', 'a/aaaaaaaaaaaaaaa'));
//return int 1
var_dump(preg_match('#((.*)*/)#i', 'a/aaaaaaaaaaaaaaaa'));
//return boolean false
?>
Whereas the regular expression is the same, and what changes is only the text that is being evaluated.
On the first line of code returns true, the faster and the second returns false, and slower ... coming to crash if placed in loop
There is an explanation for this, or is BUG
Trying other compliladores the result is the same
In Chrome console for example, this expression put the high processor clock
/((.*)*\/)/.test('a/aaaaaaaaaaaaaaaaaaaaaaaa');
In Flex, again... the same problem
I make this question because I had a code that some input parameters work, and others do not .. then simplified expression for the simplest point to demonstrate the error and got this case I'm showing.
Related
everyone. I saw a PHP while loop example today that I don't quite understand.
Example (Complete code)
// Open XML file
$fp=fopen("note.xml","r");
// Read data
while ($data=fread($fp,4096)) { // Line about which I have questions
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
The expression $data=fread($fp,4096) inside the while loop parentheses doesn't seem to change at all. It is just ONE assignment statement. How does this loop end?
The function xml_parse($parser,$data,feof($fp)) may end when parsing is complete, but I don't see how it will affect the test condition for the while loop. I feel the parsing of the XML file would repeat indefinitely.
Also when there is assignment expression as a test condition for a loop, are we really just looking to see if the RIGHT side of the assignment yields TRUE or FALSE to determine whether to end the loop? I have doubt about this though because then it would produce an infinite loop for this example since $data, once assigned, will always return TRUE.
Thanks
It ends when the fread() function returns false, which does when it reaches the end of the file. You can confirm this in the PHP documentation for fread:
http://php.net/fread
As for your question about the evaluation of the assignment, in PHP when you evaluate an assignment it will return the final value of the variable (different to Javascript for instance, in which the assignment is indeed what becomes evaluated), so you're basically both assigning a value to a variable from a function and evaluating the result of that function (assigned to the variable) without any extra syntax.
Work
while(there is data to read) OR (!EOF)
break when reach (EOF==true) OR no data to read OR return false
* EOF (end of file)
As php manual says, the function fread returns the read string or FALSE on failure.
That means:
$data=fread($fp,4096)
In the above statement the value of $data is either the contents of your file note.xml or false.
As you may know, while loop will continue until the condition is false. This statement is false only when fread($fp,4096) function fails to read data from the file, when there is no data left to read.
I ran into the snippet online
https://www.quora.com/Why-is-PHP-hated-by-so-many-developers
when I was doing some research about PHP, and I simply have no idea how the codes work.
Can anyone kindly explain what happens in the snippet and how one can log in without knowing the password?? or just give me some relevant articles to read. Thanks in advance.
See the manual:
Returns ... 0 if they [strings] are equal.
So, by the snippet logic, you should compare 0 to 0 in the end. But when you send password[]=wrong, you actually send an array, forcing strcmp to throw a warning, completely bypassing the function call and perceive the condition as true
You should always use strict comparison, just in case. So in the snippet above it would be enough to compare strictly by type and value (with ===):
if(strcmp($POST['password'], "sekret") === 0)
In this case password[]=wrong would not work anymore.
Can any one explain me following construct.
I do googling for this about 2 hours but can't understand.
public function __construct($load_complex = true)
{
$load_complex and $this->complex = $this->getComplex();
}
See: http://www.php.net/manual/en/language.operators.logical.php
PHP uses intelligent expression evaluation. If any of AND's operands evaluates to false, then there is no reason to evaluate other, because result will be false.
So if $load_complex is false there is no need to evaluate $this->complex = $this->getComplex();
This is some kind of workaround, but I do not suggest to use it, because it makes your code hard to read.
Specifically to your example $this->complex = $this->getComplex() if and only if $load_complex is set to true.
LIVE DEMO
NOTE: If any one of OPERAND result becomes 'false' in short
circuit AND evaluation means, the part of statement will be
OMITTED because there is no need to evaluate it.
Dont code like below line because, you may get probably logical
error while you are putting Expression instead of assigning values
to the variable on LEFT HAND SIDE...
$load_complex and $this->complex = $this->getComplex();
I have modified below with conditinal statement for your needs...
if($load_complex and $this->complex) {
$this->getComplex();
}
I was studying some code I found on the web recently, and came across this php syntax:
<?php $framecharset && $frame['charset'] = $framecharset; ?>
Can someone explain what is going on in this line of code?
What variable(s) are being assigned what value(s), and what is the purpose of the && operator, in that location of the statement?
Thanks!
Pat
Ah, I just wrote a blog post about this idiom in javascript:
http://www.mcphersonindustries.com/
Basically it's testing to see that $framecharset exists, and then tries to assign it to $frame['charset'] if it is non-null.
The way it works is that interpreters are lazy. Both sides of an && statement need to be true to continue. When it encounters a false value followed by &&, it stops. It doesn't continue evaluating (so in this case the assignment won't occur if $framecharset is false or null).
Some people will even put the more "expensive" half of a boolean expression after the &&, so that if the first condition isn't true, then the expensive bit won't ever be processed. It's arguable how much this actually might save, but it uses the same principle.
&& in PHP is short-circuited, which means the RHS will not be evaluated if the LHS evaluates to be false, because the result will always be false.
Your code:
$framecharset && $frame['charset'] = $framecharset;
is equivalent to :
if($framecharset) {
$frame['charset'] = $framecharset;
}
Which assigns the value of $framecharset as value to the array key charset only if the value evaluates to be true and in PHP
All strings are true except for two: a string containing nothing at all and a string containing only the character 0
in Yii they use this code:
defined('YII_DEBUG') or define('YII_DEBUG',true);
i've never seen anyone write like this before (or). is this real php code or some syntax of yii?
Basically if defined("YII_DEBUG") evaluates to false, it will then define it. Kinda like:
mysql_connect() or die("DIE!!!!!");
It is actual PHP syntax, just not commonly used. It allows you to not have to write:
if(!defined("YII_DEBUG"))
{
define("YII_DEBUG", true);
}
or even shorter
if(!defined("YII_DEBUG"))
define("YII_DEBUG", true);
I'm guessing they used it to get rid of the if statement altogether. The second if statement without brackets would be a hazard for editing, and the first one might have taken up too much room for the developer.
Personally, I'd stick clear of this just because it isn't a commonly known feature. By using commonly known syntaxes (the if statements), other programmers don't have to wonder what it does.
(Although, I might now that I look at it. Seems straightforward and gets rid of unnessecary if clauses)
It's due to short circuit evaluation.
If defined('YII_DEBUG') returns false, it will try to evaluate the second expression to make the sentence true, defining YII_DEBUG constant as true.
The final result is that, if the constant were not defined, then define it as being true. If it's already defined (the value doesn't matter), then do nothing (as the first expression is true, the second expression does not need to be evaluated for the expression to be true).
Pretty straightforward - the 'or' operator is efficient in that it will only evaluate the second part of the statement if it needs to. So if the first part evaluates to true (the constant is already defined), then the define() call isn't executed.