fightstarr20
fightstarr20

Reputation: 12598

PHP use variables in an array

Currently I am setting an array up like this...

public function setup_array()
{
    $columns = array(
        'date1' => '2014-01-24',
        'date2' => '2014-02-14',
        'date3' => '2014-03-11',
        'date4' => '2014-04-01'
    );
    return $columns;
}

This works but what I would like to use variables that are already set in place of the dates like so...

public function setup_array()
{
    $columns = array(
        'date1' => '$date1',
        'date2' => '$date1',
        'date3' => '$date1',
        'date4' => '$date1'
    );
    return $columns;
}

I have tried to do this an although the $date variables are avaliable and set, it actually prints $date1 instead or retrieving the variable value itself.

What am I doing wrong?

Upvotes: 0

Views: 1205

Answers (6)

juanmf
juanmf

Reputation: 2004

Also note that \DateTime does not have a __toString() method, so if $date1 is an instance of \DateTime, you must call $date1->format('Y-m-d')

public function setup_array($date1)
{
    $columns = array(
        'date1' => $date1->format('Y-m-d'),
        'date2' => $date1->format('Y-m-d'),
        'date3' => $date1->format('Y-m-d'),
        'date4' => $date1->format('Y-m-d'),
    );
    return $columns;
}

otherwise it'd throw an \Exception in the event of echoing or casting to string.. if $date1 is scalar you can use double quotes "$date1" and it should still work. See [http://www.php.net/manual/es/class.datetime.php]

Upvotes: 0

Manwal
Manwal

Reputation: 23816

Remove Single quotes

public function setup_array()
{
    $columns = array(
        'date1' => $date1,
        'date2' => $date1,
        'date3' => $date1,
        'date4' => $date1
    );
    return $columns;
}

Explanation:

$expand=1;$either=2;
echo 'Variables do not $expand $either';
// Outputs: Variables do not $expand $either

echo "Variables do not $expand $either";
// Outputs: Variables do not 2 1

More Details String PHP

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157862

You need to understand PHP syntax

Quotes in your first example don't mean "something that is used as array value" but regular string delimiters. I.e. these quotes belong to strings, not to array.

Upvotes: 0

Lucas Henrique
Lucas Henrique

Reputation: 1364

Remove quotes

public function setup_array($date1)
{
    $columns = array(
        'date1' => $date1,
        'date2' => $date1,
        'date3' => $date1,
        'date4' => $date1
    );
    return $columns;
}

Upvotes: 2

Alma Do
Alma Do

Reputation: 37365

  1. Single quotes will cause to interpret string as it is, literally. use double-quotes.
  2. Without any passed parameter - $date1 is unknown inside function scope and, thus, would not be substituted.

    public function setup_array($date1)
    {
        $columns = array(
            'date1' => $date1,
            'date2' => $date1,
            'date3' => $date1,
            'date4' => $date1
        );
        return $columns;
    }
    
  3. In your case even double quotes aren't needed since you're using variable as it is.

Upvotes: 4

Burhan Çetin
Burhan Çetin

Reputation: 676

Remove quotes

    public function setup_array($date1)
{
    $columns = array(
        'date1' => $date1,
        'date2' => $date1,
        'date3' => $date1,
        'date4' => $date1
    );
    return $columns;
}

Upvotes: 1

Related Questions