ariel
ariel

Reputation: 3072

How to combine a variable with a underscore and other word?

I need to combine a $variable with a underscore and word like this:

$agent = agent id word is "declined."

$newvar = $agent_declined;

How can i do that?

Upvotes: 1

Views: 2484

Answers (4)

Scott T Rogers
Scott T Rogers

Reputation: 545

Try this:

${$variableName.'_declined'} = 'foo';

For more info, see PHP: Variable variables

Upvotes: 1

Zuul
Zuul

Reputation: 16269

Use the concatenation:

<?php

$newvar = $agent . "_declined";

?>

Read here!

Upvotes: 3

Simon Forsberg
Simon Forsberg

Reputation: 13331

Like this: $newvar = $agent . "_declined";

In PHP, you combine strings by using .

Upvotes: 3

Halcyon
Halcyon

Reputation: 57728

Like this?

$agent_declined = "foobar";
$id = "declined";
$varname = "\$agent_" . $id
$newvar = $$varname; // should give foobar

I would advise against the use of double $$, it's confusing.

Upvotes: 1

Related Questions