Reputation: 5758
I am trying to wrap a variable
in single quotes in PHP
. This is my code:
$my_post = array(
'post_title' => $orderRequestCode.' - ' . $customerName,
'post_content' => $orderContent,
'post_status' => 'draft',
'post_author' => "'".$user_ID."'",
'post_type' => 'orders'
);
Currently var_dump($my_post)
outputs:
array (size=5)
'post_title' => string '2014-06-13-15-13-52 - xxxxxxx' (length=35)
'post_content' => string 'Order Code: 2014-06-13-15-13-52
Customer Name: xxxxxxxxxxxx
Customer Email: [email protected]
Order Items:
Stock Code: Q20-50-6101 Quantity: 12
Comments:
' (length=162)
'post_status' => string 'draft' (length=5)
'post_author' => string ''1'' (length=3) <--------------- should be '1'
'post_type' => string 'orders' (length=6)
This line:
'post_author' => string ''1'' (length=3)
Needs to be:
'post_author' => string '1' (length=3)
Upvotes: 2
Views: 6387
Reputation: 360672
No, you're telling PHP to create a 3-character string:
$x = 1;
$y = "'" . $x . "'";
$y = ' 1 '
1 2 3
Since it's a string, it will get wrapped by OTHER '
as well when you do your dump. If your ID is an integer, and you want it to be treated as a string, then you could something as simple as:
'post_author' => (string)$user_ID // cast to string
or
'post_author' => '' . $user_ID; // concatentate with empty string
Upvotes: 3
Reputation: 41428
The outside quotes in your var_dump
aren't really part of the string, hence the length being 3, not 5. If you echo your string it will be '1'
Upvotes: 4