Treehouse: Grow your CSS skills. Land your dream job.

if “certain” variable exist… do this.?

  • # December 29, 2011 at 2:37 pm
    < ?php
    // for example: thispage.php?word=abracadabra

    if ( $val = $_GET )
    echo "the word is: $val";

    echo "";


    That is the php im using to display certain text on a page, but my issue is, i want to display certain text if the variable =”something specific”

    example.. .com/thispage.php?word=hello so, if word = hello then echo “this text” if it does not =hello, then dont do nothin…

    i dont want someone to be able and type anything after the = sign and have something display, only of what it i that = is what i specify.. heh :) i think i think that makes sense

    any help preeease?

    # December 29, 2011 at 3:06 pm

    Something like this?

    < ?php

    $val = $_GET;

    if ($val == 'Hello') {
    echo "this text";
    // other code for people who know the secret word is "Hello"
    else {
    // do nothing

    # December 29, 2011 at 3:10 pm

    You could even cut out the middle man and not use the $val if it’s not needed. Also, since you don’t want to do anything if it doesn’t equal hello, you won’t need the ‘else’ statement either.

    < ?php

    if ($_GET == 'Hello') {
    echo "this text";
    // other code for people who know the secret word is "Hello"
    # December 29, 2011 at 3:33 pm

    but I would put a htmlspecialchars() around the $_GET for security reasons

    # December 29, 2011 at 5:49 pm

    You could also do:

    < ?php
    echo isset($_GET) && $_GET == 'Hello' ? 'Enter Text You want to Echo' : '' ;

    by using isset you are ensuring that the $_GET actually exists and is not null:

    # December 29, 2011 at 6:46 pm


    Shouldn’t it rather be:

    < ?php
    isset($_GET) && $_GET == 'Hello' ? echo 'Enter Text You want to Echo'; ?>

    That way it only performs the echo if it exists. If it doesn’t exist, there’s no need to echo anything and doesn’t slow the page.

    # December 29, 2011 at 6:52 pm

    It works the same way in mine, even with the echo in front, it is saying to only echo “Enter Text You want to Echo” if $_GET isset and it equals Hello, else don’t echo anything. It’s the PHP Ternary Operator.

    # December 30, 2011 at 1:29 am

    i <3 you all..

    31M1k97, thanks for the tip, but i am pretty much php-tarded what do you mean “around” i feel like id spend hours just trying to figure out what you said to do for the security reason.

    Senff, noahgelman, mikes02 thank you muches. i havnt tried either yet but non the less thank you very much

    # December 30, 2011 at 12:15 pm


    He means like this that when you want to use $_GET you should use it like this htmlspecialchars($_GET)

    This prevents the user from manually typing in code in the url which could break your code and/or be a security risk (although I don’t know all that much about php security best practices)

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.