NameSilo

PHP Loop - confused!

SpaceshipSpaceship
Watch

linuxfan

Established Member
Impact
0
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

IN THER WORDS:

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. Thnx in advnce.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
all that does is echo out

1 times 2 is 2
2 times 2 is 4
3 times 2 is 6
4 times 2 is 8
5 times 2 is 10
6 times 2 is 12
7 times 2 is 14
8 times 2 is 16
9 times 2 is 18
10 times 2 is 20
11 times 2 is 22
12 times 2 is 24

that

so it goes through and adds 1 to counter to 1 can become 2 then does the next bit then does the bit aftere that

the $counter* bit is where it beforms the maths

if your printing text needs to be closed off otherwise php will try and parse it and it will just die

what that does it does this

print "some text here".(PEFORM SOME PHP)."echo the rest";

bascically you can break the print up if you need to beform a query or you can do it like this

$awnser = somephpfunction/code

echo "the awnser is $awnser";

if u get me
 
0
•••
Hey. Thanks for your very thorough reply. Do you mean that the . and . are something like open and close tags that tell php that what is inside the 'tags' are what it needs to do? ( an expression )

"$counter times 2 is ".($counter*2)."<br>"
 
0
•••
i think i sorta get it now. You mean that text that is to be printed has to be closed off with " and ". so its

"text here".programhere."text continued"

like that???
 
0
•••
Originally posted by linuxfan
i think i sorta get it now. You mean that text that is to be printed has to be closed off with " and ". so its

"text here".programhere."text continued"

like that???


thats the way i understand it

the . just tell php that another action is going to be peformed so the print isnt finshing because you dont have the semo colon to show show that the print is done with
 
0
•••
thanks for helping

Thanx a lot for helping out :p
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back