PHP if else condition

somebody help me please,

<?php if ($_POST['jobs'] == 6 || 7 || 8 || 9 && $_POST['sex'] = 'L') {*true statement*} ?>

it is correct? CMIIW. someone please explain how to write if else with array condition. Thanks

Upvotes: 0

Views: 143

Answers (6)

Graham S.
Graham S.

Reputation: 163

I would do this:

$jobs = array(6, 7, 8, 9);

if ((in_array($_POST['jobs'], $jobs) && (strcmp($_POST['sex'], 'L') == 0)) {
    //Do super cool stuff
}

Upvotes: 0

Ankur Bhadania
Ankur Bhadania

Reputation: 4148

try this way

<?php 
    $string="6 || 7 || 8 || 9";
    $newarray=explode('||',$string); // $newarray is like array('6', '7', '8', '9');
    if (in_array($_POST['jobs'],$newarray) && $_POST['sex'] == 'L') {*true statement*} 
?>

Upvotes: 0

Jafar Akhondali
Jafar Akhondali

Reputation: 1537

You used one equal sign instead of two here :

$_POST['sex'] = 'L'

so that the program always see the condition true and set value of $_POST['sex'] to 'L' .

Upvotes: 0

arif_suhail_123
arif_suhail_123

Reputation: 2509

Try this You have to give value each time when you use ||

<?php if ($_POST['jobs'] == 6 || $_POST['jobs'] == 7 || $_POST['jobs'] ==8 
        || $_POST['jobs'] ==9 && $_POST['sex'] = 'L') {} 
        ?>

Upvotes: 0

Shemil R
Shemil R

Reputation: 49

If $_POST['jobs'] contain only one value, make array with all expected results. After that you can use in_array function for checking whtr the value contains in that array or not.

$resultArray = array('6', '7', '7', '8');
if (in_array($_POST['jobs'], $resultArray ) && $_POST['sex'] == 'L')
{
   //true condition
}

Upvotes: 0

Sledmore
Sledmore

Reputation: 290

I'd advise you to check out the in_array function.

$jobs = array('6', '7', '8', '9');
if (in_array($_POST['jobs'], $jobs) && $_POST['sex'] == 'L')
{
    //Do something.
}

Upvotes: 3

Related Questions