473,231 Members | 1,889 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,231 software developers and data experts.

Print and array.

Hello,

Should be a simple question... Do you know why the following two lines
print not "123" but only "1"?

$aaa[2] = 123;
print $aaa[2];

Jul 17 '05 #1
12 2235
JDS
On Tue, 05 Jul 2005 11:51:55 -0700, opt_inf_env wrote:
Should be a simple question... Do you know why the following two lines
print not "123" but only "1"?


They don't

--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Jul 17 '05 #2
OK. The following code prints 1 instead of expected 123. Even more, the
problem is in the second line. If I remove it everything is OK. Why?

<?php
session_start();
?>
<html>
<body>
<?php
$aaa[2] = 123;
print $aaa[2];
?>
</body>
</html>

Jul 17 '05 #3
op*********@yahoo.com wrote:
Hello,

Should be a simple question... Do you know why the following two lines
print not "123" but only "1"?

$aaa[2] = 123;
print $aaa[2];


I think you are missing some code here... The statement and sample are
contradicting one another.

--
Martijn
http://www.sereneconcepts.nl
Jul 17 '05 #4


op*********@yahoo.com wrote:
OK. The following code prints 1 instead of expected 123. Even more, the
problem is in the second line. If I remove it everything is OK. Why?

<?php
session_start();
?>
<html>
<body>
<?php
$aaa[2] = 123;
print $aaa[2];
?>
</body>
</html>


Is this the exact code that doesn't work? The code above works fine for
me.

Ken

Jul 17 '05 #5
I chacked it many times. It is what it is. I have only one file with
several lines which have been copied-pasted by mouse to here... No
matter what is after "$aaa[2] =" , "print $aaa[2]" always prints the
first symbol of it...

Jul 17 '05 #6
> Is this the exact code that doesn't work?
Yes.

Jul 17 '05 #7
One more detail. I have this problem only if variable name is "aaa", if
I change it by something else code works as it should work.

Jul 17 '05 #8
Problem disappeared after I prohibited the usage of cookies. It means
that cookies contained information about variable aaa. But anyway I do
not have a complete understanding of situation because before to print
value of variable I have redefined it. Moreover, why new created php
file tries to read something from some cookie? And how does it choose
from which cookie to read information?

Jul 17 '05 #9
I noticed that Message-ID:
<11*********************@z14g2000cwz.googlegroups. com> from
op*********@yahoo.com contained the following:
I chacked it many times. It is what it is. I have only one file with
several lines which have been copied-pasted by mouse to here... No
matter what is after "$aaa[2] =" , "print $aaa[2]" always prints the
first symbol of it...

Not here...
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #10


op*********@yahoo.com wrote:
Problem disappeared after I prohibited the usage of cookies. It means
that cookies contained information about variable aaa. But anyway I do
not have a complete understanding of situation because before to print
value of variable I have redefined it. Moreover, why new created php
file tries to read something from some cookie? And how does it choose
from which cookie to read information?


I think I've come up with a senario that would cause what you're
seeing.
First, is register_globals turned on? If it is, then PHP will convert
any indices in the $_COOKIE superarray to a variable, so if there was a
$_COOKIE['aaa'] with a string value, you end up with $aaa having a
string value.

Somewhere before the code snippet you posted gets executed the variable
"$aaa" is set to a string. Then when you reference $aaa[2], you are
really referencing the third character in that string (start counting
characters at 0), not the index "2". Since it was a string, PHP
converts your number, 123, into the string "123".

The following code demonstrates this:

<?php
session_start();
?>
<html>
<body>
<?php
$aaa = "123";
$aaa[2] = 123;
print $aaa[2].' '.$aaa;
?>
</body>
</html>

What prints is "1 121".

Ken

Jul 17 '05 #11
OK, now I understand! Thank you! Actually one even does not need to
speak about session. One need just remember that "a[1] = 123" does
not necessarily set the second element of array "a", "a" can be
not array at all but just a variable of string type in this case
assignment will change the second symbol in the string.

Jul 17 '05 #12
<op*********@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
OK, now I understand! Thank you! Actually one even does not need to
speak about session. One need just remember that "a[1] = 123" does
not necessarily set the second element of array "a", "a" can be
not array at all but just a variable of string type in this case
assignment will change the second symbol in the string.


....Uh...... A "String" **IS** an array of char!!!!!

Shelly
Jul 17 '05 #13

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

Similar topics

3
by: opt_inf_env | last post by:
Hello, What is a common way to print elements of a multidimensional array? I treid to do it in the straightforward way: $input_list = "Something"; print "$input_list"; And it does not work. ...
1
by: praba kar | last post by:
Dear all, In Php array_slice function base we can print array contents as per our desire eg) $a = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); $arr = array_slice($a,10,10); this function...
4
by: Shailesh | last post by:
Hi! I want to overload << operator so that it can print an arry defined in MyClass.My problem is that I want to print only a no of elements NOT all the elements in the array and this no of...
2
by: mordac | last post by:
Hello, I was wondering if I could get some opinions on how best to handle printing in a max heap data structure. Right now my heap struct looks as thus: typedef struct heapStruct { int*...
12
by: Peter Lin | last post by:
Hey, I am just wondering if anyone has got any idea of setting up a new class so that you could just print like the old ways with the printer class, since I am writing a program that really...
0
by: frack78 | last post by:
I have written a binary search algorithm in java. I have a statement that when the search key is found it is printed to a terminal window. This statement when when called just keeps printing over and...
1
by: Steff | last post by:
I am wandering if my code is making sense... I use a lot the print function. Is it weird in this case where I have to display an array ? I thought it would be better to have the entire array in php...
4
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the...
6
by: mattmao | last post by:
Okay, this is just my exercise in order to prepare for the coming assignment regarding the damned Linked List issue... The task is simple and I am about to finish it. However, I couldn't go around...
7
by: anklos | last post by:
Hi~! I met a problem on printing out a kind of hash array. i.e @array=(1,2,3,4,5,6); $array{$term}=1; diffent $i may contain diifernt $term(more than one term for each $i)
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.