| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
Dr. Macromedia
|
This should be easy for you all. This is just a small chunk of code and should take no time to figure out for PHP experts. I'm also just starting out in PHP. $counter = 1; while ( $counter <= 12 ) { print "$counter times 2 is ".($counter*2)."<br>"; $counter++ } Basically, I don't know what the ".($counter^2)."<br>" thing does. From what I understand.. First Line : $counter is declared and set the value of "1" Second Line : Whenever the while statement expression is true. That is, whenver $counter contains a number that is smaller than or equal to 12, the below code block enclosed in {} will run on and on. Third line : I don't understand that. its like all gibberish to me. From what I currently know, it prints ( or outputs ) the value inside the " " parentheses. And counter*2 is definitely the variable $counter x 2 ( multplied by 2 ) Fourth Line: Increment ( add 1 ) to counter Last line : End code block |
|
|
|
|
|
#2 (permalink) |
|
I Ain't Losing Any Sleep™
Join Date: Apr 2003
Posts: 5,240
|
Well mik, I mean gwx, here you go. In PHP $counter = 1 while ( $counter <= 12 ) { print "$counter times 2 is ".($counter*2)."<br>"; $counter++ } In Plain English Counter = 1 If 1 is less than or equal to 12 Print - 1 times 2 is 2 Add 1 to Counter Loop again... Counter now equals 2 If 2 is less than or equal to 12 blah blah blah That's fuckin' ingenious, if I understand it correctly. It's a Swiss fuckin' watch.
|
|
|
|
#3 (permalink) |
|
Dr. Macromedia
|
Thanks smallbeer, but I still don't really get part of it. PHP is asked to print "$counter times 2 is ".($counter*2)."<br>" I understand that $counter would evaluate to 1 Thus, PHP is to print 1 times 2 is ".($counter)*2."<br> What does the brackets enclosing $counter above do? I'm not sure of what the dot do, but its supposed to connect the left and right strings together. Therefore, 1 times 2 is "($counter)^2}"<br> What is that that pair of " signs do? and what does the bracket tell php to do? Is it possible that the " " signs tell php that the stuff inside are to be evaluated ( is an expression )? I'm so confused. |
|
|
|
#8 (permalink) |
|
I Call Shenanigans™
Join Date: Feb 2003
Location: Manchester
Posts: 9,651
|
please dont encourage him gwx , go here - http://www.programmingtalk.com , Ive put in a good word for you. |
|
|
|
#9 (permalink) | |
|
Dr. Macromedia
|
Quote:
Hi. Thanks for the link! |
|
|
|
|
#10 (permalink) | |
|
Registered User
Join Date: Jun 2003
Posts: 6
|
Quote:
The full stop allows you to add something to a string in PHP. So assuming $counter is 2... PHP Code:
will actually write Code:
Effectively, everything inside the " " is echosed to the screen, but you obviously want to calculate the value of $counter*2 before you echo it, so you close the brackets, add the evaluated answer to the string (using the .) and then finish the string off with a <br> tag. IF you try this: Code:
then PHP Code:
will display Code:
whereas PHP Code:
will display Code:
or PHP Code:
will display Code:
Not explained in an even remotely eloquent fashion... but it's the best I can do. Friday avo, and Thursday night drinkies are starting to take their toll... Phil |
|
|
|
|
#11 (permalink) |
|
Iris Folder
Join Date: Apr 2003
Location: smokey
Posts: 2,676
|
phil, does the php actually evaluate the $counter in the string or just print it? "$counter times 2 is ".($counter*2)."<br>" to $counter times 2 is 4 <br> ? I know this is how it would work in Actionscritp / javascript, but it's been a while since I did any PHP - bit rusty. |
|
|
|
#12 (permalink) | |
|
Registered User
Join Date: Jun 2003
Location: Belfast
Posts: 43
|
Quote:
As far as I know (still getting into PHP myself at the mo) it needs to evaluate the statement inside the brackets before it can echo it... |
|
|
|
|
#13 (permalink) |
|
Iris Folder
Join Date: Apr 2003
Location: smokey
Posts: 2,676
|
S'ok I tested it, and my result cuncured with yours. I wasn't asking about the multiplied $counter but the $ counter contained in the first part of the statement - "$counter times 2 is" - anyways the answer is that it does evaluate it even though it's in a string and I was wrong. yay me |
|
![]() |