1

I am creating a website and I need to to some PHP in my jQuery.

My code is as follows;

notifications.initMenu({
    2:'#chatPage #tabs #2',
    3:'#chatPage #tabs #3',
    4:'#chatPage #tabs #4',
    5:'#chatPage #tabs #5'
});

I want it so that it uses a while loop to echo;

$pageID:'#chatPage #tabs #$pageID',

And then on the last record echo;

$pageID:'#chatPage #tabs #$pageID'

Any ideas? Thanks.

1
  • 1
    Show us the PHP code you have already! Commented Jul 24, 2011 at 19:19

2 Answers 2

7
$array = array();
while( ....) {
    $array[] = "\n  $pageID:'#chatPage #tabs #$pageID'";
}
echo implode(',', $array);
Sign up to request clarification or add additional context in comments.

4 Comments

just for curiosity, which one is faster, $array[] or array_push()?
@Simon: Obviously $array[].
@Simon: According to a test conducted at the following page (stackoverflow.com/questions/559844/…), assigning variables using $array[] seems to be up to 50% faster than using array_push().
the manual also mentions that it's faster for one element, which is a bit weird because a good compiler could very easily detect that it's just one element passed and emit the same bytecode, but oh well, php... anyway, this is tipically below the level of the optimisation that you have to worry about
1

If the comma is what you ask about:

for ($i = 2; $i <= 5; $i++) {
    echo "$i:'#chatPage #tabs #$i'" . ($i !== 5 ? "," ? "") . "\n";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.