If statement inside an if statement dilemma [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 4 years ago.
Improve this question
I have the following code, but I have no idea what I'm doing wrong. Any idea how to fix this?
<?php
if($testing = series_get_meta('code_name')) {
if ($testing == 'Pre1';) {
echo "Pre1";
} elseif ($testing == 'Gem2';) {
echo "Gem2";
} elseif ($testing == 'Remi1';) {
echo "Remi1";
} else {
echo "Default";
}
}
?>

You have a couple typos.
Don't use semi colons in the () of your if/else statements.
You have an extra parenthesis at the end of your last else statement.
Like so:
if ($testing == 'Pre1')
{
echo "Pre1";
} elseif ($testing == 'Gem2') {
echo "Gem2";
} elseif ($testing == 'Remi1') {
echo "Remi1";
} else {
echo "Default";
}
However you task is probably better suited to using a switch() statement like so:
switch ($testing) {
case 'Pre1':
echo 'Pre1';
break;
case 'Gem2':
echo 'Gem2';
break;
case 'Remi1':
echo 'Remi1';
break;
default:
echo 'Default';
}

Hey you have added semicolon inside the if condition, just remove that
<empty>

Related

PHP nested if using $_GET [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 1 year ago.
Improve this question
Trying to use a $_GET within a $_GET
It's not throwing any errors but isn't working the way I want it to. It's been a while since I've done this so I don't know if I'm overlooking something or not. I'm trying to be able to do something like index.php?chatroom&id=1
$pgtitle = '';
$cractive = '';
$dactive = '';
$acactive = '';
$pgChat = '';
if(isset($_GET['chatroom'])){
$cractive = 'active';
if (isset($_GET['cid']) == "1") {
$pgChat == 'Global Chatroom';
}else if(isset($_GET['cid']) == "2"){
$pgChat == 'AK Chatroom';
}else if(isset($_GET['cid']) == "3"){
$pgChat == 'AZ Chatroom';
} else {
echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
}
}else{
header('Location: index.php?dashboard');
}
isset() returns a boolean not the content of the variable, and you was using == instead of = in $pgChat == 'Global Chatroom';, here is an example using your code for something like index.php?chatroom&cid=1:
if (isset($_GET['chatroom'])) {
$cractive = 'active';
if (isset($_GET['cid'])) {
if ($_GET['cid'] == "1") {
$pgChat = 'Global Chatroom';
} elseif ($_GET['cid'] == "2") {
$pgChat = 'AK Chatroom';
} elseif ($_GET['cid'] == "3") {
$pgChat = 'AZ Chatroom';
} else {
echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
}
} else {
echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
}
}
EXTRA: You can do the same thing in that way if you wanna.
$chats = [
'1' => 'Global Chatroom',
'2' => 'AK Chatroom',
'3' => 'AZ Chatroom',
];
if (isset($_GET['chatroom'])) {
$cractive = 'active';
if (isset($_GET['cid']) && isset($chats[$_GET['cid']])) {
$pgChat = $chats[$_GET['cid']];
} else {
echo '<meta http-equiv="refresh" content="0; URL=index.php?chatroom&cid=1">';
}
}

where to write else condition inside loop within another loop? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
If the phone number matches , I am able to print "Yes". If it doesnot match , i need to print "No". But if i include else part , its executing only else part. Please guide me how and where to write else part.
<?php if($re['phone_v'] == "1"){
echo "Yes";
}else if($re['phone_v'] == "0"){
for($i=0;$i < count($crusers);$i++) {
if ($re['phone'] == $crusers[$i]['phone']) {
echo "Yes";
}
}
}
?>
I am checking condition inside table in view. small thing is making complicated.
This will help you.
$re['phone_v'] = 0;
$re['phone'] = 1;
$crusers[0]['phone'] = 1;
if($re['phone_v'] == "1"){
echo "Yes";
}else{
if($re['phone_v'] == "0")
{
for($i=0;$i < count($crusers);$i++)
{
if ($re['phone'] == $crusers[$i]['phone'])
{
echo "Yes11";
}else{
echo "No";
}
}
}
}

Simple PHP issue that I cant seem to get to 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 8 years ago.
Improve this question
Im working on a simple roll a dice program. So it has to generate a random number from 0-5 and when it selects 0-4 it echo's and when it selects 5 it echo's.
To it keeps echo the first echo 'you got 6'. I cant seem to get my mind around this.
<!DOCTYPE html>
<html>
<head>
<title>A loop of your own</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<?php
$trows = 0;
$dice = 0;
while ($dice == 0)
{
$dice = rand(0,5);
$trows++;
}
if ($dice = 5)
{
echo 'you got 6';
}
else
{
echo 'you got 1-5';
}
echo '<br><br>';
echo $trows;
?>
</body>
if ($dice = 5)
Is actually assigning the value of 5 to dice, rather than comparing the values, so the first part of the if statement will be entered, rather than the else.
if ($dice == 5)
is what you are looking for
Edit: From your comments I think this is what you are looking for
$trows = 0;
$dice = 0;
while ($dice != 5)
{
$dice = rand(0,5);
$trows++;
if ($dice == 5) {
echo 'you got 6';
} else {
echo 'you got 1-5';
}
echo '<br><br>';
echo $trows;
}
Your if statement is not testing for a condition, but rather performing an assignment and then evaluating the truth of that assignment (which will always be true in this case).
Your code should be:
if($dice == 5)
{
echo 'you got 1-5';
}
Notice the == instead of the single =
This will do the trick
while ($trows == 0)
{
$dice = rand(0,5);
if ($dice == 5)
{
echo 'you got 6';
}
else
{
echo 'you got 1-5';
$trows++;
}
}

why isnt this equal function working [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
im trying to make something to change password.
nkode is the new password
gkode is the old password
if($_GET[rediger] == 'ja'){
$nkode = md5($_POST[nkode]);
$gkode = md5($_POST[gkode]);
if($nkode !== ''){
if($gkode !== ''){
$nukode = $udskrivprofil[Kodeord];
if($gkode == '$nukode'){
echo "success";
} else {
echo "fail";
}
}
}
echo "<br>$gkode <br> $nukode";
}
both $gkode and $nukode outputs the 100% same, yet i get the fail error... whats wrong?
if($gkode == '$nukode'){
Look at those quotes. You're comparing the contents of $gkode against a string which has the characters $, n, u, etc... in it...
Maybe you wanted
if($gkode == $nukode){
instead?
remove quotes from $nukode variable...
if($_GET[rediger] == 'ja'){
$nkode = md5($_POST['nkode']);
$gkode = md5($_POST['gkode']);
if($nkode !== ''){
if($gkode !== ''){
$nukode = $udskrivprofil[Kodeord];
if($gkode == $nukode){
echo "success";
} else {
echo "fail";
}
}
}
echo "<br>$gkode <br> $nukode";
}

Anybody have a more efficient way to write this php function? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I was just practicing and wanted to see if there was a more robust way to write this? Should I use a for loop?
<?php
function recursion($number){
if ($number === 0){
break;
}
else {
$subtracted_number = $number-1;
echo $subtracted_number;
recursion($subtracted_number);
}
}
recursion(9);
?>
//output is 876543210
as you can do it in a simple for loop you won't need your recursion here
function countdown($num)
{
for($i=$num-1; $i>0; $i--)
{
if($i !=0)
echo $i;
}
}
use as:
countdown(9);//echoes 87654321
function recursion($number){
$number--;
if ($number > 0){
echo $number;
recursion($number);
}
}
Just for fun http://codepad.org/4S4BcofG
function recursion($number) {
while ($number > 1) {
$number--;
echo $number;
}
}

Categories