473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

PHP assiging a variable to an array

Claus Mygind
571 512MB
Do you encapsulate a variable with " " when assigning a variable to an array.


Which of these is correct

$x = array("foo" => '"'.$bar.'"');

or

$x = array("foo" => $bar);

All examples shown are always constants

I would assume option #2 is the correct method.
Jul 16 '12 #1
6 19654
Rabbit
12,516 Expert Mod 8TB
This is easy to test. Code both and test the results.
Jul 16 '12 #2
Claus Mygind
571 512MB
Yes but I do not have an easy way to display results. Question do you know which is correct?
Jul 16 '12 #3
Rabbit
12,516 Expert Mod 8TB
Technically the second. The first will work as well as long as the variable is a string.
Jul 16 '12 #4
The first one is just a string concatenation, so that the value of $bar would be converted into string. Here is the output for $bar=2;

Expand|Select|Wrap|Line Numbers
  1. $x => Array (1)
  2. (
  3. |    ['foo'] = String(3) ""2""
  4. )
  5. $x => Array (1)
  6. (
  7. |    ['foo'] = Integer(1) 2
  8. )
  9.  
As you see the second one takes the value of $bar as it is. More clearly you can see the difference if $bar is an array like :
Expand|Select|Wrap|Line Numbers
  1. $bar = array("drink" => "milk", "desert" => "pudding");
  2.  
The output is :
Expand|Select|Wrap|Line Numbers
  1. $x => Array (1)
  2. (
  3. |    ['foo'] = String(7) ""Array""
  4. )
  5. $x => Array (1)
  6. (
  7. |    ['foo'] => Array (2)
  8. |    (
  9. |    |    ['drink'] = String(4) "milk"
  10. |    |    ['desert'] = String(7) "pudding"
  11. |    )
  12. )
  13.  
Jul 16 '12 #5
Dormilich
8,658 Expert Mod 8TB
Which of these is correct

$x = array("foo" => '"'.$bar.'"');

or

$x = array("foo" => $bar);
both are correct. the difference is that the first one is always a string and that it is 2 chars (preceding and following ") longer.
Jul 17 '12 #6
Claus Mygind
571 512MB
Thanks for all the great responses.
Jul 18 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Neil Jarman | last post by:
Hi, I need to create an array with a session variable. According to thebook I'm using, the following should work, but if fails: for each key in request.QueryString if left(key,6) = "cboRun"...
1
by: BCC | last post by:
If I have a class with the following member var: std::string* m_array; Is there a way to initialize it in the constructor using new or anything else in this fashion: m_array = new std::string...
13
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
9
by: portroe | last post by:
by the way, how do you deal with a variable array lenth, for example if the array depends on the number of years 'input' by the user, Dim Years(?) As Integer thanks
6
by: echoking | last post by:
I have a program that asks a user for a integer. This integer is a member of a struct statement. Later on in the program a function points to this structure in hopes of using the integer as a...
5
by: farrell.niall | last post by:
Hi, I'm trying to use nested foreach loops to loop through two arrays. Each element in the first array corresponds to another array. The brandcode part of the first array is the same as the name...
6
by: brainlpb | last post by:
I'm trying to adapt a PHP shopping cart code to ASP: The cart outputs the SQL product list to a HTML a page where the client enters the quantity they want of each SKU: <FORM ACTION="page.asp"...
3
by: Keriana30 | last post by:
I need to base a variable array using a variable. For example, ReDim pstrDepSSN(pintRecordCount) as string The only way to do this is with the ReDim statement but it doesn't permit me to...
1
by: potterb | last post by:
I am trying to setup a attribute that will have multiple values in it. ViewObject voc1d = service.findViewObject("GetCommercialNeighbor"); ViewCriteria vcc1d =...
0
by: PauloD | last post by:
public struct Character_Items { public short Armor; public short Helm; public short WeaponL; public short WeaponR; public short Pants; public short Boots; public short Gloves; public short...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.