Balderick Posted May 22, 2017 Share Posted May 22, 2017 Sorry people here I have asked this before, so its a bit embarassing to me. But .... I have the following question. Its about using a function inside an if condition. The thing is that when I make a function with 2 returns in this case $data and $alternative I receive them both back. I ask this for grasping how a function acts inside an if condition. So my question is: how does the php engine ‘reason’? Is the outcome of the return ONLY POSSIBLE if you return 1 value inside an if condition; so either true or false ? Example: <?php function valid_someting($data){ global $alternative; $count=''; $count = strlen($data); if ( ($count < 8 && ($count > 4) ) ) { $data = preg_replace('/[^a-zA-Z0-9!@#$%^&*()\-_=+{};:,<.>]/', '', $data ); // $data = ltrim($data); $data = rtrim($data); return $data; } $alternative = $data; return $alternative; } $data = 'abcdabcdabcd'; if (valid_someting($data)) { echo 'color input field green'; } else if ($alternative == TRUE) { echo 'color input field RED'; } ?> Link to comment Share on other sites More sharing options...
dsonesuk Posted May 22, 2017 Share Posted May 22, 2017 (edited) IF you are saying 'color input field green' is returned if string length is inside or outside the if condition, it is because a string always returns true (unless "0"), while comparing with '==' a string will always be TRUE, but first if condition takes priority. Check out http://php.net/manual/en/types.comparisons.php. Edited May 22, 2017 by dsonesuk Link to comment Share on other sites More sharing options...
dsonesuk Posted May 22, 2017 Share Posted May 22, 2017 IF you are checking if string length is valid and return false or true <?php function valid_someting($data){ $count = strlen($data); $valid = false; if ( ($count < 8 && ($count > 4) ) ) { $data = preg_replace('/[^a-zA-Z0-9!@#$%^&*()\-_=+{};:,<.>]/', '', $data ); // $data = ltrim($data); $data = rtrim($data); $valid = $data; } return $valid; } $data = 'abcdabcdabcd'; //$data = 'abcddd'; if (valid_someting($data)) { echo 'color input field green<br>'; } else{ echo 'color input field RED<br>'; } Set false to variable returned, if valid in if condition, reset it to true (or include string, but resetting to true would be best) Link to comment Share on other sites More sharing options...
justsomeguy Posted May 22, 2017 Share Posted May 22, 2017 If you're asking more generally about multiple return statements in a function, a return statement causes the function to stop executing at that point and return the value back to the calling scope. Multiple return statements will not be executed because function execution stops whenever a return statement is reached. Link to comment Share on other sites More sharing options...
dsonesuk Posted May 23, 2017 Share Posted May 23, 2017 Valid result: The value is processed and returns with 'return $data;' and stops going any further. The $alternative variable has no set value because of this and so is NULL. So red warning won't proceed any further. Invalid result: The process skips if condition and processes $alternative and returns with 'return $alternative;'. BUT! the '$alternative' elseif is specifically targeting a boolean type of TRUE, this will never pass this as its type is 'string' not boolean. The if condition 'if (valid_someting($data)) {' is different it checks the value of returned data, and being a just a string it will always return TRUE unless it equals '0', because it is not specific to a boolean type, it can be string, integer, or array. IT will work if you use 'if(is_string($alternative))' but it will return one of the function return values, plus the $alternative set value, meaning both will be processed in the if and elseif conditions. Check out example below <?php //$alternative="empty"; function valid_someting($data){ global $alternative; echo ' current $alternative value: <br>'; echo 'boolean value BEFORE IF: ' . getType($alternative) . ' "' . $alternative . '"<br><br>'; $count=0; $count = strlen($data); if ( ($count < 8 && ($count > 4) ) ) { $data = preg_replace('/[^a-zA-Z0-9!@#$%^&*()\-_=+{};:,<.>]/', '', $data ); // $data = ltrim($data); $data = rtrim($data); $data.='aaaa'; return $data; } $alternative = $data; echo 'boolean value AFTER IF: ' . getType($alternative) . ' "' . $alternative . '"<br><br>'; $alternative.='bbbb'; return $alternative; } $data = 'abcdabcdabcd'; //$data = 'abcda'; $result=valid_someting($data); if ($result) { echo 'boolean value of $data function IF: ' . getType($result) . ' "' . $result . '"<br><br>'; echo 'color input field green'; } if ($alternative==TRUE) { echo 'color input field RED'; } echo '<br><br>boolean value of $alternative AFTER ELSEIF: ' . getType($alternative) . ' "' . $alternative . '"<br>'; The best option is in previous example, where you return a true or false, to compare with if or else. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now