4thSpace
4thSpace

Reputation: 44312

How to use $_GET in a string?

I have a simple PHP file with the following:

<?php
echo 'catid=$_GET["catid"]';
<?>

When I run the page, the output is:

catid=$_GET["catid"]

I'm accessing the page as www.abc.com/temp.php?catid=3. I'd like $_GET to execute so I see:

catid=3

What am I doing wrong?

Upvotes: 1

Views: 197

Answers (7)

Jordi Kroon
Jordi Kroon

Reputation: 2597

There are a few options to combine a variable with a string.

<?php

    $var = "something";

    // prints some something
    echo 'some ' . $var; // I prefer to go for this one

    // prints some something
    echo "some $var";

    // prints some $var
    echo 'some $var';

    // prints some something
    echo "some {$var}";

?>

Upvotes: 0

Rolice
Rolice

Reputation: 3103

You have to use double quoted string to notify PHP that it might contains variables inside. $_GET is array, so you will need to put the variable statement in {}.

<?php
    echo "catid={$_GET['catid']}";
?>

Upvotes: 0

GGio
GGio

Reputation: 7653

The best way to use variables in string is:

echo "catid={$_GET['catid']}";

Upvotes: 0

syrkull
syrkull

Reputation: 2344

$_Get is a variable, and to echo a variable you do not need parenthesis around it.

<?php
echo 'catid='.$_GET["catid"];
?>

please see this : source

Upvotes: 1

ToXyS
ToXyS

Reputation: 31

You may try:

echo "catid= {$_GET['catid']}";

Upvotes: 0

th3falc0n
th3falc0n

Reputation: 1427

You can use non-array variables for that:

$getCatID = $_GET["catid"];
echo "catid=$getCatID";

Or you can use (recommended):

echo 'catid=' . $_GET["catid"];

Upvotes: 0

insertusernamehere
insertusernamehere

Reputation: 23580

You have to cancat the two:

echo 'catid=' . $_GET["catid"];

or you could use " (double quotes):

echo "catId=$someVar";

Upvotes: 2

Related Questions