Is IF obligatory to be followed by ELSE [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am not sure if the IF must be followed by ELSE like in my example above or if IF can be used alone like it is without any ELSE.
Will the page show to anyone if there is no ELSE? I am a beginner...
<?php
$rank = $user["rank"];
if ($rank != 'ADMIN'){
header('Location: get_the_he.._out.php');
exit();
}
else {
?>
<html>
<head><title>Show Page</title></head>
<body>
Here we show page if user is admin
</body>
</html>
<?php } ?>

NO. There is no need to have an ELSE with an IF. However you then need to make sure that the script will not continue after the IF, as you are doing here :)

Related

PHP JSON If or Else [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to write a PHP script so if the network type is a JSON feed, I'm pulling indicates mobile event is triggered and if not another event is triggered. I can't figure out the if or else, I've tried a bunch of variations.
Code:
echo( "Network Type: " . $decoded_response['current_carrier']['network_type']);
if ($decoded_response['current_carrier']['network_type']) = 'mobile') {
echo "Event1";
} else {
echo "Event2";
}
if ($decoded_response['current_carrier']['network_type']) = 'mobile'){
maybe this should be
if ($decoded_response['current_carrier']['network_type'] == 'mobile'){
You are using single = instead of == to check for equality. = would assign and not test.

Checking userlevel but it won't work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm not the best at php and mysql and i'm still learning however i have this userlevel variable which i define in my session
and if i message it back to myself using
<?php echo"$session->userlevel"; ?>
It'll work, it'll tell me that me that my userlevel is "0" which it should be however when i'm using an if statement to check it won't work?
<?php
if ($_SESSION['userlevel'] = 0) {
echo "Userlevel 0 was found!";
}
?>
Any though
if($_SESSION['userlevel'] = 0)
should be
if($_SESSION['userlevel'] == 0)
Otherwise you don't check, you assign the value 0 to $_SESSION['userlevel']

HTML PHP if else statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have the following part of code inside my HTML body tag:
<?php if(1==2) { ?>
<a class="demage" href="xxxx.html">demage</a>
<a class="history" href="xxxx.html">history</a>
<?php } else { ?>
<a class="Sign In" href="login_page.html">Sign In</a>
<a class="Sign Up" href="register_page.html">Sign Up</a>
<?php } ?>
but it displays the 4 links in a row, not the last two as it is supposed to. What is wrong with the code?
Answered in comments, collective effort.
By default PHP will only evaluate/parse files containing PHP with the extension
.php
I don't see any error in the code the code is running as expected. (See the image attached.)
May be there's some problem with your php.

$_GET does not equal variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
So, I'm making a profile page, and I need to have it so it displays other content if it's not your username in the URL. How would i go about doing this, considering this doesn't seem to be working.
else if(isset($_GET[!$myUsername])){
echo "hi";
}
Thank You!
Its probably a bad idea to pass a username in the url ($_GET holds the url parameters)
I would start by reading up on php sessions here is a quick tutorial:
http://www.tizag.com/phpT/phpsessions.php
The exact answer to your question is is the placement of the the "!", it needs to be moved in front of isset().
if (!isset($_GET['myUserName'])) {
//this is true when ?mysuername= is not present in the address bar.
}
After you read up on the sessions you will handle this by setting a a session variable, something names "loggedIn" or similar.
if (isset($_SESSION['loggedIn'])) {
//Show Logged IN Content
echo "Your user id is: ". $_SESSION['userId'];
} else {
//Show not logged in content
echo "You should probably log in before trying to look at this content";
}

Header always redirect: [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When admin log in to the admin panel it should redirect him to the admin panel and the second time to not, but it always redirecting.
if($logedin_admin='1'){
$logedin_admin=0;
$_SESSION['is_admin']=1;
header('Location: admin/index.php');
}
Can anyone tell me why?
I've made the if:
if(isset($logedin_admin) && $logedin_admin=='1'){
echo 'dsdasdas';
$logedin_admin=0;
$_SESSION['is_admin']=1;
header('Location: admin/index.php');
exit;
}
but it isn't redirecting and it isn't echoing dsdasdas
It should be if($logedin_admin=='1'){
You are doing an assignment operation instead of a comparison..
Also, make sure if the session was started...
The code..
<?php
session_start(); //<=--------- Add this
if($logedin_admin=='1'){
$logedin_admin=0;
$_SESSION['is_admin']=1;
header('Location: admin/index.php');
exit; //<=----------- Add an exit too.
}

Categories