Jump to content

I hate if and if else . how to write it


egturnkey

Recommended Posts

Hello friends,

 

If we have the following

 

$case1 = "11111";
$case2 = "2222";
$ad_one = "love";
$ad_two = "hate";
$ratio = "40";

srand ((float) microtime() * 10000000);
$random = rand(1, 100);

 

 

i wanna say the following manic problems

 

if ($ad_two == $case1 || $ad_two == $case2) {
echo $ad_one;
} else {

if ($random>= $ratio) {
echo $ad_one;
}
else {
echo $ad_two;
}

 

but it shows error (syntax error, unexpected $end)

 

i know there is something wrong in if,else . can anyone please re-write it !  :'(

 

Link to comment
https://forums.phpfreaks.com/topic/195505-i-hate-if-and-if-else-how-to-write-it/
Share on other sites

Proper indenting will help you with these problems.

 

Here is your code, poorly indented:

<?php
if ($ad_two == $case1 || $ad_two == $case2) {
echo $ad_one;
} else {

if ($random>= $ratio) {
echo $ad_one;
}
else {
echo $ad_two;
}
?>

 

Here is your code, indented in some manner:

<?php
if ($ad_two == $case1 || $ad_two == $case2) {
    echo $ad_one;
} else {
    if ($random>= $ratio) {
        echo $ad_one;
    } else {
        echo $ad_two;
    }
// <- With proper indenting, we see clearly that you're missing a closing bracket
?>

 

Fixed.

<?php
if ($ad_two == $case1 || $ad_two == $case2) {
    echo $ad_one;
} else {
    if ($random>= $ratio) {
        echo $ad_one;
    } else {
        echo $ad_two;
    }
}
?>

 

You should invest in an IDE that highlights matching brackets.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.