BB Design
BB Design

Reputation: 703

How to setup an array in PHP with a variable inserted

I am an ASP programmer trying to write PHP, so my apologies if this is really basic. I have a variable $fname and I am trying to setup an array as follows:

$data=array('x_First_Name' => '" . str_replace("'" ,"\'", $fname) . "');

But, I get an error: unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')'

Something simple in my syntax that needs to be adjusted? Thanks!

Upvotes: 0

Views: 38

Answers (2)

Charlotte Dunois
Charlotte Dunois

Reputation: 4680

If you have only a variable or a function, you don't need to add quotes. And to 'break' the quotes for a function or a variable, you need to use the same quotes.

$data=array('x_First_Name' => str_replace("'" ,"\'", $fname));

Upvotes: 1

showdev
showdev

Reputation: 29168

You don't need to quote the value:

$data=array('x_First_Name' => str_replace("'" ,"\'", $fname) );

You might also find PHP's addslashes() helpful:

$data=array('x_First_Name' => addslashes($fname));

Upvotes: 5

Related Questions