1

In javascript my if else statement goes like this:

var x = document.getElementById('input1').value;
var y = document.getElementById('input2').value;
if (x.length > 0 && y.length > 0) {
    //execute code
}

This is what I have soo far with php:

$varx = $_POST['input1'];
$vary = $_POST['input2'];
if () {
    //execute code
}

I have no idea how to convert x.length to a php variable. It would help me alot if someone knows how to do this.

I really want to have the if else statement in php so please don't come up with other solutions because I'll get even more confused. Because this code will be a part of a bigger project.

2
  • 1
    What about strlen($varx)? Commented Aug 2, 2016 at 17:04
  • 1
    Or, since the JS code only seems to check that both variables actually contain some value you could simplify that to if ($varx && $vary) because of the way php will evaluate that expression. An empty string evaluates to false in php, a non empty string to true. Commented Aug 2, 2016 at 17:05

1 Answer 1

3

You can use below syntax for checking that variable is having value or not.

if (null != $varx && null != $vary) {
    // your code
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it helped alot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.