PDA

View Full Version : ${finalprice.$i+1}



leonidassavvides
08-16-2008, 07:57 PM
this is correct:

<?= ${finalprice.$i+1} ?>
to get
$finalprice1
$finalprice2
$finalprice3
...
...
in a loop ?
what this syntax is being called ?

thetestingsite
08-16-2008, 08:16 PM
Not sure if I understand you correctly, but are you asking about a for or while loop? If so, what exactly is the question?

Twey
08-16-2008, 09:05 PM
There are two things to point out here. The first is the <?= ?> syntax, which is now deprecated and should no longer be used. The other is the use of 'variable variables' (in answer to your question), which is very bad practice and always indicative of bad design. Avoid those, too.

motormichael12
08-17-2008, 02:14 AM
Instead of

$finalprice1
$finalprice2
$finalprice3

I do either of the following:

$finalprice[]
$finalprice[]
$finalprice[]

or

$finalprice[1]
$finalprice[2]
$finalprice[3]

Then use:

for($i=0;$i<count($finalprice);$i++);
{
echo "finalprice {$i}: {$finalprice[$i]}
}

Returns things like this:

finalprice 1: value
finalprice 2: value
finalprice 3: value

With my site's user system I recently found this out:

$user = (object) mysql_fetch_array($sql);

I can now pull data using $user->id or $user->username, etc. makes it look cool and I find that easier than $user['username']

Twey
08-17-2008, 04:50 AM
Handy tip. Yes, arrays are a much better alternative.

Note here: one of the examples a) has syntax errors, and b) would be better expressed with a foreach loop.
// With proper syntax
for ($i = 0; $i < count($finalprice); $i++)
echo "finalprice {$i}: {$finalprice[$i]}";

// With a foreach
foreach ($finalprice as $k => $v)
echo "finalprice $k: $v";

motormichael12
08-17-2008, 01:52 PM
Handy tip. Yes, arrays are a much better alternative.

Note here: one of the examples a) has syntax errors, and b) would be better expressed with a foreach loop.
// With proper syntax
for ($i = 0; $i < count($finalprice); $i++)
echo "finalprice {$i}: {$finalprice[$i]}";

// With a foreach
foreach ($finalprice as $k => $v)
echo "finalprice $k: $v";

I dpon't remember where but I read somewhere that you should akways use brackets in loops and that the way you have would be bad syntax, is that true?

And also what if you need to do multiple lines in the loop? then you would need the brackets, right?

Twey
08-17-2008, 04:05 PM
No. All flow-control statements of this kind in C-like languages are of a general format:
keyword (flow control specifier(s)) statementIf you wish to execute multiple statements at once, it's possible to combine them into one statement using a block:
{
theseStatements();
willAllBe($treated) * $syntactically;
AsOne::statement();
}Certain schools of programming (not ones for which I hold a great amount of respect) hold that it is preferable to always use a block, since it makes it easier to a) see what each block is delimiting, and b) add extra lines if it becomes necessary later. However, I believe that a) can be accomplished with indentation (very successfully; indeed, this is the only way of doing it in Python), and while b) is a small plus, it's never difficult to add a block in if necessary, and one spares one's self an astonishing amount of junk by not doing so in the first place, in a big project. In addition, it helps one to remember where to create separate, more general functions -- if you have to use a block, perhaps that loop body should be generalised and moved into another function. This helps to create clean, neat, reusable code.

Block scope is also a factor to consider in some languages, but not in PHP, which does not have block scope.

leonidassavvides
08-19-2008, 10:14 AM
arrays in php need to be declared first ?
I want array(16)
setup as
off[1]=
....
off[2]=
....
off[3]=
....other code(all code in a block , same page)
....
off[16]=

Twey
08-19-2008, 06:08 PM
Er, yes...
$off = array('item1',
'item2',
'item3');

// ...

$off[3] = 'item4';Don't forget that arrays are zero-indexed. I think that perhaps you would benefit from a PHP tutorial (http://uk.php.net/tut.php).