Jump to content

if statement with an empty or unasigned verable


crawlerbasher

Recommended Posts

<php
$id = strip_tags($_GET['id']);

if ($id) {
echo "No assigned verable or entry verable";
} elseif ($id = "Test") {
echo "This is a Test Page";
}
?>

 

Now would a if ($id) take the user to the no assigned verable if the verable in the url is empty on not asigned a verable of Test?

 

so if index.php?id=Test takes them to this is a test page

but index.php or index.php?id=gobledegoop take them to no assigned verable or entry.

Link to comment
Share on other sites

You'd preferrably use the isset function. Here is a (shouldbe) working example:

<?php 
if (isset($_GET['id'])) {
    $id = htmlentities(strip_tags($_GET['id'])); //Should really be this..
    if ($id == "test") {
        echo "Welcome to the test page! ID = test";
    } else {
        echo "ID is not valid, please re-enter"; //If ID = blahwhatever
    }
} else {
    echo 'ID is not defined.';
}
?>

Link to comment
Share on other sites

<php
$id = strip_tags($_GET['id']);

if ($id) {
echo "No assigned verable or entry verable";
} elseif ($id = "Test") {
echo "This is a Test Page";
}
?>

 

Now would a if ($id) take the user to the no assigned verable if the verable in the url is empty on not asigned a verable of Test?

 

so if index.php?id=Test takes them to this is a test page

but index.php or index.php?id=gobledegoop take them to no assigned verable or entry.

 

try this one:

 

<?php

$id = strip_tags($_GET['id']);

 

if($id="test1"){

    $id="test1.php";

}else{

  $id="test2.php";

}

?>

<div ><a href="?id=test1">menu test 1</a></div>

<div ><a href="?id=test2">menu test 2</a></div>

 

//include what page will be appear

<?php require_once($id); ?>

 

 

Hope it helps...

Link to comment
Share on other sites

try this one:

 

<?php

$id = strip_tags($_GET['id']);

 

if($id="test1"){

    $id="test1.php";

}else{

  $id="test2.php";

}

?>

<div ><a href="?id=test1">menu test 1</a></div>

<div ><a href="?id=test2">menu test 2</a></div>

 

//include what page will be appear

<?php require_once($id); ?>

 

 

Hope it helps...

 

I'd highly unrecommend using this code, as it is a direct entry into a mass amount of problems.

Link to comment
Share on other sites

try this one:

 

<?php

$id = strip_tags($_GET['id']);

 

if($id="test1"){

    $id="test1.php";

}else{

  $id="test2.php";

}

?>

<div ><a href="?id=test1">menu test 1</a></div>

<div ><a href="?id=test2">menu test 2</a></div>

 

//include what page will be appear

<?php require_once($id); ?>

 

 

Hope it helps...

 

I'd highly unrecommend using this code, as it is a direct entry into a mass amount of problems.

 

Well the codes of bombsquad and oni-kun is correct, but unnecessary. Well with the code of oni-kun, it used a long method, while with bombsquad's code he's missing one function the isset().

 

Let's analyze the problem:

 

[*] Is the $_GET["id"] set?

[*] If set, is the $_GET["id"] equal to Test?

 

Now, if we make it into coding this would be the solution...

 



if(isset($_GET["id"]) && ($id = htmlentities(strip_tags($_GET['id']))) == "Test") {
echo "This is a Test Page";
} else {
echo "No assigned verable or entry verable";
}

 

Explanation:

- If $_GET["id"] is not set, result would be "No assigned verable or entry verable".

- If we've got set $_GET["id"] but not equal to Test, result would still be "No assigned verable or entry verable".

to make it simplier.

 

- $_GET["id"] should be set and equal to "Test". Which is equal to this condition..

if(isset($_GET["id"]) && ($id = htmlentities(strip_tags($_GET['id']))) == "Test")

- any other situation that does not fall into this condition will have a result "No assigned verable or entry verable".

And at the same time, we set and clean/validate the variable $id.

Link to comment
Share on other sites

Well with the code of oni-kun, it used a long method

 

I'd have to say your explanation of code is hardly worth noting, as you're taking my code and compressing it into something E_STRICT would throw an warning on. Shorter = better?

Link to comment
Share on other sites

Doing such long method could make the next programmer hard to understand the logic of your codes. And that is my, "The way of my programming."

 

I posted a simple, commented and proper practise of programming that is taught by the manual, you posted a compressed, shortcutted code that returns a PHP warning.  How is that helpful to the OP?

 

..but i've learned a lot

Clearly not from a good source.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.