Simple PHP IF/ELSE code not working properly with html markup [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
The 3 different equals
5 answers
I have started to learn some basic PHP. So far everything worked perfectly until I tried to run some IF/ELSE statements embedded into the markup as you can see in the snipet bellow:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
The result is always the first if statement, even if it doesn't meet the condition. In this case the result is:
0
5
Tried everything I knew and searched a lot without any results. What am I doing wrong here?
php html
marked as duplicate by Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 20 '18 at 14:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
The 3 different equals
5 answers
I have started to learn some basic PHP. So far everything worked perfectly until I tried to run some IF/ELSE statements embedded into the markup as you can see in the snipet bellow:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
The result is always the first if statement, even if it doesn't meet the condition. In this case the result is:
0
5
Tried everything I knew and searched a lot without any results. What am I doing wrong here?
php html
marked as duplicate by Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 20 '18 at 14:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
YOU missed=
in your if condition. it should be==
.
– Hamza Zafeer
Nov 16 '18 at 13:28
Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use===
– Klors
Nov 16 '18 at 13:35
add a comment |
This question already has an answer here:
The 3 different equals
5 answers
I have started to learn some basic PHP. So far everything worked perfectly until I tried to run some IF/ELSE statements embedded into the markup as you can see in the snipet bellow:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
The result is always the first if statement, even if it doesn't meet the condition. In this case the result is:
0
5
Tried everything I knew and searched a lot without any results. What am I doing wrong here?
php html
This question already has an answer here:
The 3 different equals
5 answers
I have started to learn some basic PHP. So far everything worked perfectly until I tried to run some IF/ELSE statements embedded into the markup as you can see in the snipet bellow:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
The result is always the first if statement, even if it doesn't meet the condition. In this case the result is:
0
5
Tried everything I knew and searched a lot without any results. What am I doing wrong here?
This question already has an answer here:
The 3 different equals
5 answers
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg = 5){ ?>
<h1>5</h1>
<?php } elseif ($msg = 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
php html
php html
asked Nov 16 '18 at 13:25
Jo.joeJo.joe
72
72
marked as duplicate by Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 20 '18 at 14:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 20 '18 at 14:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
YOU missed=
in your if condition. it should be==
.
– Hamza Zafeer
Nov 16 '18 at 13:28
Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use===
– Klors
Nov 16 '18 at 13:35
add a comment |
4
YOU missed=
in your if condition. it should be==
.
– Hamza Zafeer
Nov 16 '18 at 13:28
Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use===
– Klors
Nov 16 '18 at 13:35
4
4
YOU missed
=
in your if condition. it should be ==
.– Hamza Zafeer
Nov 16 '18 at 13:28
YOU missed
=
in your if condition. it should be ==
.– Hamza Zafeer
Nov 16 '18 at 13:28
Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use
===
– Klors
Nov 16 '18 at 13:35
Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use
===
– Klors
Nov 16 '18 at 13:35
add a comment |
4 Answers
4
active
oldest
votes
This is a simple syntax error, it needs to be:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
Notice the ==
which compares things vs. =
which sets things.
add a comment |
The =
sign is for assignment; in order to do a comparison you need to use ==
:
<?php if ($msg == 5){ ?>
add a comment |
The problem is in the condition -> ($msg = 5)
It should be ==
($msg == 5) instead of ($msg = 5).
When you are comparing two values you need to use double equal sign not single.
Single means you are assigning value to the variable.
Don'g give up!!
Good luck!
1
Hi, we normally just comment on a typo and close the question. They are almost never any use to others
– RiggsFolly
Nov 16 '18 at 13:32
add a comment |
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
3
Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.
– Nicky Mattsson
Nov 16 '18 at 13:59
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a simple syntax error, it needs to be:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
Notice the ==
which compares things vs. =
which sets things.
add a comment |
This is a simple syntax error, it needs to be:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
Notice the ==
which compares things vs. =
which sets things.
add a comment |
This is a simple syntax error, it needs to be:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
Notice the ==
which compares things vs. =
which sets things.
This is a simple syntax error, it needs to be:
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
Notice the ==
which compares things vs. =
which sets things.
answered Nov 16 '18 at 13:28
AdamAdam
1,05121520
1,05121520
add a comment |
add a comment |
The =
sign is for assignment; in order to do a comparison you need to use ==
:
<?php if ($msg == 5){ ?>
add a comment |
The =
sign is for assignment; in order to do a comparison you need to use ==
:
<?php if ($msg == 5){ ?>
add a comment |
The =
sign is for assignment; in order to do a comparison you need to use ==
:
<?php if ($msg == 5){ ?>
The =
sign is for assignment; in order to do a comparison you need to use ==
:
<?php if ($msg == 5){ ?>
answered Nov 16 '18 at 13:28
TordekTordek
7,16122961
7,16122961
add a comment |
add a comment |
The problem is in the condition -> ($msg = 5)
It should be ==
($msg == 5) instead of ($msg = 5).
When you are comparing two values you need to use double equal sign not single.
Single means you are assigning value to the variable.
Don'g give up!!
Good luck!
1
Hi, we normally just comment on a typo and close the question. They are almost never any use to others
– RiggsFolly
Nov 16 '18 at 13:32
add a comment |
The problem is in the condition -> ($msg = 5)
It should be ==
($msg == 5) instead of ($msg = 5).
When you are comparing two values you need to use double equal sign not single.
Single means you are assigning value to the variable.
Don'g give up!!
Good luck!
1
Hi, we normally just comment on a typo and close the question. They are almost never any use to others
– RiggsFolly
Nov 16 '18 at 13:32
add a comment |
The problem is in the condition -> ($msg = 5)
It should be ==
($msg == 5) instead of ($msg = 5).
When you are comparing two values you need to use double equal sign not single.
Single means you are assigning value to the variable.
Don'g give up!!
Good luck!
The problem is in the condition -> ($msg = 5)
It should be ==
($msg == 5) instead of ($msg = 5).
When you are comparing two values you need to use double equal sign not single.
Single means you are assigning value to the variable.
Don'g give up!!
Good luck!
answered Nov 16 '18 at 13:31
Marcin - user2676388Marcin - user2676388
564
564
1
Hi, we normally just comment on a typo and close the question. They are almost never any use to others
– RiggsFolly
Nov 16 '18 at 13:32
add a comment |
1
Hi, we normally just comment on a typo and close the question. They are almost never any use to others
– RiggsFolly
Nov 16 '18 at 13:32
1
1
Hi, we normally just comment on a typo and close the question. They are almost never any use to others
– RiggsFolly
Nov 16 '18 at 13:32
Hi, we normally just comment on a typo and close the question. They are almost never any use to others
– RiggsFolly
Nov 16 '18 at 13:32
add a comment |
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
3
Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.
– Nicky Mattsson
Nov 16 '18 at 13:59
add a comment |
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
3
Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.
– Nicky Mattsson
Nov 16 '18 at 13:59
add a comment |
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
<?php
$msg = 0;
echo $msg;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<?php if ($msg == 5){ ?>
<h1>5</h1>
<?php } elseif ($msg == 0) { ?>
<h1> 0 </h1>
<?php } else { ?>
<h1>Whatever</h1>
<?php } ?>
</body>
</html>
answered Nov 16 '18 at 13:38
Rm khanRm khan
92
92
3
Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.
– Nicky Mattsson
Nov 16 '18 at 13:59
add a comment |
3
Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.
– Nicky Mattsson
Nov 16 '18 at 13:59
3
3
Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.
– Nicky Mattsson
Nov 16 '18 at 13:59
Please include a bit of explanation. Especially on the difference between your code and Jo.joe's.
– Nicky Mattsson
Nov 16 '18 at 13:59
add a comment |
4
YOU missed
=
in your if condition. it should be==
.– Hamza Zafeer
Nov 16 '18 at 13:28
Also, if you are interested to know if the type is correct, rather than just the value after type juggling, then use
===
– Klors
Nov 16 '18 at 13:35