0

im not sure on how i am going to explain this correctly.

I wanted a function to validate a string which i figured correctly.

But i want the function to return a boolean value.

And outside a function i need to make a condition that if the function returned false, or true that will do something. Here's my code.

i am not sure if this is correct.

<?php
$string1 = 'hi';
function validatestring($myString, $str2) {
    if(!empty($myString)) {
        if(preg_match('/^[a-zA-Z0-9]+$/', $str2)) {
            }
    }
    else {
        return false;
    }
}

if(validatestring == FALSE) {
    //put some codes here
}
else {
    //put some codes here
}
?>

EDIT : Now what if there are more than 1 condition inside the function?

<?php
$string1 = 'hi';
function validatestring($myString, $myString2) {
    if(!empty($myString)) {
        if(preg_match('/^[a-zA-Z0-9]+$/', $str2)) {
            return true;
        }
        else {
            retun false;
        }
    }
    else {
        return false;
    }
}

if(validatestring($myString, $myString2) === FALSE) {
    //put some codes here
}
else {
    //put some codes here
}
?>
3
  • 1
    if(validatestring($myString)){ ? Commented Mar 20, 2014 at 6:10
  • you are not passing argument to function ... Commented Mar 20, 2014 at 6:10
  • 2
    and use === instead of == Commented Mar 20, 2014 at 6:10

4 Answers 4

2

Functions need brackets and parameter. You dont have any of them.

This would be correct:

if(validatestring($myString) === false) {
    //put some codes here
}

An easier and more elegant method would be this:

if(!validatestring($myString)) {
    //put some codes here
}
Sign up to request clarification or add additional context in comments.

7 Comments

so, i won't need to call the function first? e.g validatestring($myString); if(//the condition) {}
The function will be evaluated first because the value is needed to check for the if. The value which the functions returns will be used in the condition. So you dont have to call it first, no, the above method is valid, common and more professional than evaluating the function first, save it in a variable and then use the variable in the condition.
so since it is needed for the if then the function will be evaluated without calling it then make the if, was that right?
You are calling the function INSIDE the if condition. This is like you were calling it outside. It would be even possible to assign the variable inside the if like if(($var = validatestring($myString)) === false) {}.
Thats valid yes. A function can only return once, after it, it aborts and will continue with the usual code. So you can put millions of return in a function, it will ALWAYS only return one value.
|
1
<?php
$string1 = 'hi';
function validatestring($myString) {
    if(!empty($myString)) {
        return true;
    }
    else {
        return false;
    }
}

if(validatestring($string1) === FALSE) {
    //put some codes here
}
else {
    //put some codes here
}
?>

Comments

1

Sidenote, since empty() already returns false ,you could simplify by doing:

function validateString($string){
  return !empty($string);
}

if(validateString($myString){
  // ok
}
else {
 // not ok
}

To make a check and test later:

 $check = validateString($myString);

 if($check){ }

There's no need to check == false or === false, the function already returns a boolean, it would be redundant.

Comments

-1

store $string1 to $myString in the function myString=string1

<?php
$string1 = 'hi';
function validatestring($myString) {
        myString=string1;
    if(!empty($myString)) {
        return true;
    }
    else {
        return false;
    }
}

if(validatestring() === FALSE) {
    //put some codes here
}
else {
    //put some codes here
}
?>

1 Comment

What??? Apart from the syntax error, $string1 is undefined inside the scope of the function!

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.