473,509 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array bugs?

7h

Sorry if I sounds like a noob (because I am one, here). I ran across
this earlier, and it kept bugging me. I guess someone here know a good
answer.

To summerize, I was using array within a class. After some
manipulation, the value of the array's element that I needed wasn't
"echoed" right.

For instance,

class FootballClub
{
public $name;
public $stadium = "junkyard";
public $roster = array("gk"=>"clown", "st"=>"fool");
public $coach;
}

$myclub = new FootballClub();
$myclub->name = "Liverpool";

if ($myclub->name == "Liverpool")
{
$myclub->stadium = "Anfield";
$myclub->coach = "Rafa Benitez";
$myclub->roster["gk"] = "Pepe Reina";
$myclub->roster["st"] = "Robbie Fowler";
}
$myplayer = $myclub->roster[gk];
echo "your team plays at: $myclub->stadium<br>";
echo "your goal keeper is: $myclub->roster[gk]<br>");

---------
Problem:

As is, the 2nd line output is: "your goal keeper is: Array[gk]" thought
$myplayer gets the right value.

Any plausible explaination, please?

Thanks.

Aug 6 '06 #1
7 1332
"7h@ch" <es****@gmail.comwrote:
echo "your goal keeper is: $myclub->roster[gk]<br>");

As is, the 2nd line output is: "your goal keeper is: Array[gk]"
echo "your goal keeper is: {$myclub->roster['gk']}<br>");

Note the curly braces and quotes.

miguel
--
Photos from 40 countries on 5 continents: http://travel.u.nu
Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
Airports of the world: http://airport.u.nu
Aug 6 '06 #2
Rik
Miguel Cruz wrote:
"7h@ch" <es****@gmail.comwrote:
>echo "your goal keeper is: $myclub->roster[gk]<br>");

As is, the 2nd line output is: "your goal keeper is: Array[gk]"

echo "your goal keeper is: {$myclub->roster['gk']}<br>");

Note the curly braces and quotes.
Yup, see Complex (curly) syntax:
http://www.php.net/manual/en/languag...arsing.complex

Grtz,
--
Rik Wasmus
Aug 6 '06 #3
7h@ch wrote:
Sorry if I sounds like a noob (because I am one, here). I ran across
this earlier, and it kept bugging me. I guess someone here know a good
answer.

To summerize, I was using array within a class. After some
manipulation, the value of the array's element that I needed wasn't
"echoed" right.

For instance,

class FootballClub
{
public $name;
public $stadium = "junkyard";
public $roster = array("gk"=>"clown", "st"=>"fool");
public $coach;
}

$myclub = new FootballClub();
$myclub->name = "Liverpool";

if ($myclub->name == "Liverpool")
{
$myclub->stadium = "Anfield";
$myclub->coach = "Rafa Benitez";
$myclub->roster["gk"] = "Pepe Reina";
$myclub->roster["st"] = "Robbie Fowler";
}
$myplayer = $myclub->roster[gk];
echo "your team plays at: $myclub->stadium<br>";
echo "your goal keeper is: $myclub->roster[gk]<br>");

---------
Problem:

As is, the 2nd line output is: "your goal keeper is: Array[gk]" thought
$myplayer gets the right value.

Any plausible explaination, please?

Thanks.
Although not strictly necessary in php, always put your variables
outside of quotes. This came to me easily as I was developing in c and
C++ before I moved on to PHP.
Aug 6 '06 #4

"s a n j a y" <sa***********@gmail.comwrote in message
news:Na******************************@comcast.com. ..
7h@ch wrote:
>Sorry if I sounds like a noob (because I am one, here). I ran across
this earlier, and it kept bugging me. I guess someone here know a good
answer.

To summerize, I was using array within a class. After some
manipulation, the value of the array's element that I needed wasn't
"echoed" right.

For instance,

class FootballClub
{
public $name;
public $stadium = "junkyard";
public $roster = array("gk"=>"clown", "st"=>"fool");
public $coach;
}

$myclub = new FootballClub();
$myclub->name = "Liverpool";

if ($myclub->name == "Liverpool")
{
$myclub->stadium = "Anfield";
$myclub->coach = "Rafa Benitez";
$myclub->roster["gk"] = "Pepe Reina";
$myclub->roster["st"] = "Robbie Fowler";
}
$myplayer = $myclub->roster[gk];
echo "your team plays at: $myclub->stadium<br>";
echo "your goal keeper is: $myclub->roster[gk]<br>");

---------
Problem:

As is, the 2nd line output is: "your goal keeper is: Array[gk]" thought
$myplayer gets the right value.

Any plausible explaination, please?

Thanks.

Although not strictly necessary in php, always put your variables outside
of quotes. This came to me easily as I was developing in c and C++ before
I moved on to PHP.
Hmm. I looke at someone else's answer of putting braces in. I thought to
myself, "Great, I just learned something by reading this newgroup. Store it
away". When I saw your response I thought to myself "Why hadn't I come
across this problem before, myself?". Well, your answer showed me why. I
would have written it as:

echo "your goal keeper is:" . $myclub->roster['gk'] . "<br>";

and never have had that difficulty. My background in C and java developed
that same habit in me.

Shelly
Aug 6 '06 #5
Rik
Shelly wrote:
"s a n j a y" <sa***********@gmail.comwrote in message
news:Na******************************@comcast.com. ..
>7h@ch wrote:
>>Sorry if I sounds like a noob (because I am one, here). I ran across
this earlier, and it kept bugging me. I guess someone here know a
good answer.

To summerize, I was using array within a class. After some
manipulation, the value of the array's element that I needed wasn't
"echoed" right.

For instance,

class FootballClub
{
public $name;
public $stadium = "junkyard";
public $roster = array("gk"=>"clown", "st"=>"fool");
public $coach;
}

$myclub = new FootballClub();
$myclub->name = "Liverpool";

if ($myclub->name == "Liverpool")
{
$myclub->stadium = "Anfield";
$myclub->coach = "Rafa Benitez";
$myclub->roster["gk"] = "Pepe Reina";
$myclub->roster["st"] = "Robbie Fowler";
}
$myplayer = $myclub->roster[gk];
echo "your team plays at: $myclub->stadium<br>";
echo "your goal keeper is: $myclub->roster[gk]<br>");

---------
Problem:

As is, the 2nd line output is: "your goal keeper is: Array[gk]"
thought $myplayer gets the right value.

Any plausible explaination, please?

Thanks.

Although not strictly necessary in php, always put your variables
outside of quotes. This came to me easily as I was developing in c
and C++ before I moved on to PHP.

Hmm. I looke at someone else's answer of putting braces in. I
thought to myself, "Great, I just learned something by reading this
newgroup. Store it away". When I saw your response I thought to
myself "Why hadn't I come across this problem before, myself?".
Well, your answer showed me why. I would have written it as:

echo "your goal keeper is:" . $myclub->roster['gk'] . "<br>";

and never have had that difficulty. My background in C and java
developed that same habit in me.
It could be done like that, for sure (allthough I'd use single quotes for
text without variables & newlines). When outputting HTML tags however, it
can become a complete quote-fest, that I'd rather avoid to improve
legibility. Heredoc & curly often come to the rescue, allthough certainly
for numberformatting, but also for strings, (s/v)printf() is certainly an
outcome.

Consider:
array product{
[id] =>.....
[name] =>...
[decription] =...
[image_src] =...
.....
}

Concating way:

echo '
<h2>'.$product['id'].':'.$product['name'].'</h2>
<p>
<a href="./products/?id='.$product['id'].'"><img
src="'.$product['image_src'].'" /></a>
'.$product['decscription'].'
let\'s say way have to use singel quote\'s here... escaping?
Read more about <a
href="./products/?id='.$product['id'].'">'.$product['name'].'</a>
</p>';

Double quotes, curly:

echo "
<h2>{$product['id']}:{$product['name']}</h2>
<p>
<a href=\"./products/?id={$product['id']}\"><img
src=\"{$product['image_src']}\" /></a>
{$product['decscription']}
let's say way have to use singel quote's here... escaping?
Read more about <a
href=\"./products/?id={$product['id']}\">{$product['name']}</a>
</p>';

Heredoc, curly

echo <<<HTML
<h2>{$product['id']}:{$product['name']}</h2>
<p>
<a href="./products/?id={$product['id']}"><img
src="{$product['image_src']}" /></a>
{$product['decscription']}
let's say way have to use singel quote's here... escaping?
Read more about <a
href="./products/?id={$product['id']}">{$product['name']}</a>
</p>
HTML;

Printf possibility:

vprintf(<<<HTML
<h2>%1$03d:%2$s</h2>
<p>
<a href="./products/?id=%1$d"><img src="%4$s" /></a>
%3$s
let's say way have to use single quote's here... escaping?
Read more about <a href="./products/?id=%1$d">%2$s</a>
</p>
HTML;
, $products);

The latter one might come in handywhen outputting a list from an array
(allthough the code isn't really charming):

$list = array('page 1','page 2','page 3'....
vprintf(str_repeat("\n<li>%s</li>", count($list)),$list);

It's all about legibility, what you're actually doing, wether it has to be
easily maintained and wether it's a repeating output.

Grtz,
--
Rik Wasmus
Aug 6 '06 #6

"Rik" <lu************@hotmail.comwrote in message
news:6d***************************@news1.tudelft.n l...
Shelly wrote:
>"s a n j a y" <sa***********@gmail.comwrote in message
news:Na******************************@comcast.com ...
>>7h@ch wrote:
Sorry if I sounds like a noob (because I am one, here). I ran across
this earlier, and it kept bugging me. I guess someone here know a
good answer.

To summerize, I was using array within a class. After some
manipulation, the value of the array's element that I needed wasn't
"echoed" right.

For instance,

class FootballClub
{
public $name;
public $stadium = "junkyard";
public $roster = array("gk"=>"clown", "st"=>"fool");
public $coach;
}

$myclub = new FootballClub();
$myclub->name = "Liverpool";

if ($myclub->name == "Liverpool")
{
$myclub->stadium = "Anfield";
$myclub->coach = "Rafa Benitez";
$myclub->roster["gk"] = "Pepe Reina";
$myclub->roster["st"] = "Robbie Fowler";
}
$myplayer = $myclub->roster[gk];
echo "your team plays at: $myclub->stadium<br>";
echo "your goal keeper is: $myclub->roster[gk]<br>");

---------
Problem:

As is, the 2nd line output is: "your goal keeper is: Array[gk]"
thought $myplayer gets the right value.

Any plausible explaination, please?

Thanks.
Although not strictly necessary in php, always put your variables
outside of quotes. This came to me easily as I was developing in c
and C++ before I moved on to PHP.

Hmm. I looke at someone else's answer of putting braces in. I
thought to myself, "Great, I just learned something by reading this
newgroup. Store it away". When I saw your response I thought to
myself "Why hadn't I come across this problem before, myself?".
Well, your answer showed me why. I would have written it as:

echo "your goal keeper is:" . $myclub->roster['gk'] . "<br>";

and never have had that difficulty. My background in C and java
developed that same habit in me.

It could be done like that, for sure (allthough I'd use single quotes for
text without variables & newlines). When outputting HTML tags however, it
can become a complete quote-fest, that I'd rather avoid to improve
legibility. Heredoc & curly often come to the rescue, allthough certainly
for numberformatting, but also for strings, (s/v)printf() is certainly an
outcome.
Sometimes I use single and sometimes double if there is no necessity to
choose between them. See below.
>
Consider:
array product{
[id] =>.....
[name] =>...
[decription] =...
[image_src] =...
.....
}

Concating way:

echo '
<h2>'.$product['id'].':'.$product['name'].'</h2>
<p>
<a href="./products/?id='.$product['id'].'"><img
src="'.$product['image_src'].'" /></a>
'.$product['decscription'].'
let\'s say way have to use singel quote\'s here... escaping?
Read more about <a
href="./products/?id='.$product['id'].'">'.$product['name'].'</a>
</p>';
Here I would do the preceding without needing escapes:

echo ' <h2>' . $product['id'] . ':' .$product['name'] .
'</h2><p><a href="./products/?id=' . $product['id'] . '" ><img src="' .
$product['image_src'] . '" /></a>' . $product['decscription'] .
"let's say way have to use single quote's here... escaping?Read more
about <a " .
'href="/products/?id=' . $product['id'] . '">' . $product['name'] .
'</a></p>';

Putting in spaces between the concatonation dots greatly improves
legibility. Also, not needing the extra escape character (when it can be
avoided) also does that.
>
Double quotes, curly:

echo "
<h2>{$product['id']}:{$product['name']}</h2>
<p>
<a href=\"./products/?id={$product['id']}\"><img
src=\"{$product['image_src']}\" /></a>
{$product['decscription']}
let's say way have to use singel quote's here... escaping?
Read more about <a
href=\"./products/?id={$product['id']}\">{$product['name']}</a>
</p>';

Heredoc, curly

echo <<<HTML
<h2>{$product['id']}:{$product['name']}</h2>
<p>
<a href="./products/?id={$product['id']}"><img
src="{$product['image_src']}" /></a>
{$product['decscription']}
let's say way have to use singel quote's here... escaping?
Read more about <a
href="./products/?id={$product['id']}">{$product['name']}</a>
</p>
HTML;

Printf possibility:

vprintf(<<<HTML
<h2>%1$03d:%2$s</h2>
<p>
<a href="./products/?id=%1$d"><img src="%4$s" /></a>
%3$s
let's say way have to use single quote's here... escaping?
Read more about <a href="./products/?id=%1$d">%2$s</a>
</p>
HTML;
, $products);

The latter one might come in handywhen outputting a list from an array
(allthough the code isn't really charming):

$list = array('page 1','page 2','page 3'....
vprintf(str_repeat("\n<li>%s</li>", count($list)),$list);

It's all about legibility, what you're actually doing, wether it has to be
easily maintained and wether it's a repeating output.
I agree with this last statement 100%. That is why I always put in a spaces
where possible and end the first line with the concatonation dot to and
start the second line in the right indent position.

Shelly
Aug 6 '06 #7
7h

Thanks all.

Guess I gotta take out some of the sloppy coding habits ;-)

Aug 7 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
55478
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- ...
6
2357
by: surrealtrauma | last post by:
i have a trouble about that: i want to ask user to enter the employee data (employee no., name, worked hour, etc.), but i dont know how to sort the data related to a particular employee as a...
18
18615
by: laclac01 | last post by:
Is there a way to pass a 2d array to a function in c++ with out having to specifiy the number of elements in the array. Here is an example #include<iostream> using namespace std; int...
29
5416
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
3
2672
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
21
22888
by: vito | last post by:
how to achieve that? it seems php doesn't support it well for a C programmer? i hope to use something like: a; a; a;
8
3434
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
45
4763
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
272
13874
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
7
2704
by: kigoobe | last post by:
Hi gits thanks for your reply. I am sorry about the link, I didn't think that way. I will be more careful from now. The function as it stands now, is still giving me issues, and I need a way to...
0
7234
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
7136
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
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
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...
1
5060
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...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.