473,668 Members | 2,312 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"=>"cl own", "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 1341
"7h@ch" <es****@gmail.c omwrote:
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.c omwrote:
>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"=>"cl own", "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"=>"cl own", "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
manipulatio n, 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"=>"cl own", "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 numberformattin g, but also for strings, (s/v)printf() is certainly an
outcome.

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

Concating way:

echo '
<h2>'.$produc t['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'].'">'.$produc t['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_rep eat("\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.comwro te in message
news:6d******** *************** ****@news1.tude lft.nl...
Shelly wrote:
>"s a n j a y" <sa***********@ gmail.comwrote in message
news:Na******* *************** ********@comcas t.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"=>"cl own", "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 numberformattin g, 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>'.$produc t['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'].'">'.$produc t['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_rep eat("\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
55539
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--- #include "stdio.h" #include "string.h" void printText(char c){
6
2365
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 group. i want to use a array object in the class but i don't know how..i am just learning the c++. So i dont know how to use class. in fact, i have writen like the following: class employee { public: employee();
18
18626
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 main() { int array;
29
5448
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 elements as *(mptr+k) where I've declared MYTYPE *mptr; what should be the type of 'k'? Should it be ptrdiff_t?
3
2689
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 return an array as a property because it will return a copy of the array instead a reference to it. How can I force the property to return a reference to the array? Is it only a feature of arrays? I hope normal class objects (including collections)...
21
22913
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
3442
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 I access the array elements. Thanks for your help. #include <stdio.h>
45
4826
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 "new Array() vs " question or to the array performance, I dared to move it to a new thread. Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9 of Books of ECMA :-)
272
14011
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 dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
7
2716
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 reload a given array. I was wondering if you could help. Here is the situation, the image below is of two examples of a given array, say array_1, when I call a function. http://kigoobe.com/offshoring/bugs/javascript/reloadArray1.gif
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8378
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8577
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.