Fatal Error Cannot Break/continue 1 Level
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Fatal error: Cannot break/continue 1 level in up vote 1 down vote favorite I have a function which must check basic auth setted credentials, if user exist in file .htgroup and check their password from file .htpasswd and return 0 if no matches 1 if matches. $password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : ''; $user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : ''; function authenticate($user, $password) { $AuthUserFile = file(".htpasswd"); $AuthGroupFile = file(".htgroup"); $group = "Members"; if(!preg_grep("/$group: $user$/", $AuthGroupFile)) { if(!($authUserLine = array_shift(preg_grep("/$user:.*$/", $AuthUserFile)))) { preg_match("/$user:((..).*)$/", $authUserLine, $matches); $authPW = $matches[1]; $salt = $matches[2]; $submittedPW = crypt($password, $salt); if($submittedPW != $authPW) { return 0; } else { return 1; }; } else { return 0; }; }else { return 0; }; } I got error on if(!($authUserLine = array_shift(preg_grep("/$user:.*$/", $AuthUserFile)))) { Strict Standards: Only variables should be passed by reference in /var/www/auth.php on line 54 What is wrong? php share|improve this question asked Jul 29 '15 at 10:33 Dmitrij Holkin 8951829 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote accepted array_shift needs to have the first argument passed as reference as it needs to updated the array you pass and remove the first element. In order to make it work, you'll need to store the result of preg_grep in a variable: $matches = preg_grep("/$user:.*$/", $AuthUserFile); if(!($authUserLine = array_shift($matches))) { ... } share|improve this answer answered Jul 29 '15 at 10:40 jedrzej.kurylo 14.3k11740 add a comment| up vote 1 down vote As from PHP.ne
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PHP Fatal error: Cannot break/continue up vote 4 down vote favorite if (isset($errors)) { foreach ($errors as $error) { echo $error; } } else http://stackoverflow.com/questions/31697987/fatal-error-cannot-break-continue-1-level-in {break 2;} // some more code Outputs: Fatal error: Cannot break/continue 2 levels I tried break 1, it didn't work either. php foreach if-statement break share|improve this question edited Feb 10 '12 at 6:49 Dan D. 33.8k65679 asked Sep 1 '10 at 12:38 Georgi Georgiev 567922 4 There are no loops in your code. Please post full code. –codaddict Sep 1 '10 at 12:39 Could you please explain the context in which this http://stackoverflow.com/questions/3618030/php-fatal-error-cannot-break-continue code runs? –PJP Sep 1 '10 at 12:39 There is no need for break there because when the if fails it will continue going on regardless. –Chris Sep 1 '10 at 12:43 add a comment| 3 Answers 3 active oldest votes up vote 2 down vote accepted if (isset($errors)) { foreach ($errors as $error) { echo $error; } } No need to use break as you seem to want to end on the else condition. just use the above code for your errors, it will be skipped if no errors. No need for break share|improve this answer answered Sep 1 '10 at 12:51 Phill Pafford 40k66211329 add a comment| up vote 3 down vote Break ends the execution within a foreach, for, while, do-while or switch structure.. if (isset($errors)) { foreach ($errors as $error) { echo $error; } } else {break 2;} //there is no loop here! share|improve this answer edited Feb 5 '13 at 22:02 Kaii 11.4k21842 answered Sep 1 '10 at 12:41 Russell Dias 28.7k43261 Why the down vote? Care to explain? –Russell Dias Sep 1 '10 at 23:00 removed the false statement that break does not take an argument. –Kaii Feb 5 '13 at 22:03 add a comment| up vote 0 down vote Just type break not followed with any number. But break is he
loop Page 1 of 1 New Topic/Question Reply 5 Replies - 9939 Views - Last Post: 28 October 2010 - http://www.dreamincode.net/forums/topic/197175-fatal-error-cannot-breakcontinue-1-level-in/ 03:41 AM Rate Topic: #1 emetiib New D.I.C Head Reputation: 2 Posts: 26 Joined: 06-October 10 Fatal error: Cannot break/continue 1 level in Posted 28 October 2010 - 12:17 AM Foreach loop break works fine as written (All code between comments) Once turned into function ... get Fatal Error. Cannot break/continue fatal error 1 level. This is the index.php file
'; /** // Requested URL $url = $_SERVER['REQUEST_URI']; //echo $url.''; // Removes Application root from url $url = str_replace('/localFixers/', '', $url); echo $url. '
'; //posts/2/edit $routes = array( array('url' => '/^posts\/(?P
'; if(preg_match($route['url'], $url, $matches)) { $params = array_merge($params, $matches); echo "match found
"; $route_match = true; break; print_r ($matches); } } if(!$route_match) { exit('no route found'); } print_r ($matches); //print($route['controller']); //print('controller: '.$route['controller'].' view: '.$route['view'].' id: '.$params['id']); //echo $url; include($_SERVER['DOCUMENT_ROOT'].'/localFixers/controllers/'.$route['controller'].'.php'); */ include('config.php'); function dispatcher($routes) { // Requested URL $url = $_SERVER['REQUEST_URI']; //echo $url; // Removes Application root from url $url = str_replace('/'.APP_ROOT.'/', '', $url); //echo APP_ROOT; // holds the named captures $params = array(); // becomes true if $route ['url'] = $url $route_match = false; // loops over $routes looking for a match foreach($routes as $urls => $route) print_r ($routes); { //echo 'looking for match..
'; // if match found appends $matches to $params // sets $route_match to true and exits loop if(preg_match($route['url'], $url, $matches)) print_r ($matches); { $params = array_merge($params, $matches); //e