4

PHP newbie here

Can anyone please tell me what is wrong with the below syntax. I have a maximum of 4 files - $created_page1, $created_page2 each with a corresponding page title etc and would like to process these in a loop. However PHP throws a wobbly every time I try to concatenate the string and loop number - specifically $created_page.$num_pages doesn't result in sending $created_page1 or $created_page2 to the function, instead it just converts the string and number to an integer. Very basic I am sure but I would be very grateful for any help or a nicer solution that I can easily understand. Thanks in advance!

$addit_pages == 4;

for ($num_pages=1;$num_pages<=$addit_pages ;$num_pages++) { 

replaceFileContent   ($dir,$created_page.$num_pages,"*page_title*",$page_title.$num_pages); 
//replaceFileContent  ($dir,$created_page2,"*page_title*",$page_title2); 
//replaceFileContent  ($dir,$created_page1,"*page_title*",$page_title3); 
//replaceFileContent  ($dir,$created_page3,"*page_title*",$page_title4);    
}
5
  • I'm not clear on what you're trying to do. what is "*page_title*"? Commented May 17, 2012 at 23:44
  • 4
    How far does it throw the wobbly? Commented May 17, 2012 at 23:47
  • and whats the content of these parameters ? i guess you are mixing up DATE and INTEGER or something like that !? Commented May 17, 2012 at 23:48
  • Please, learn to use arrays instead of numbered variables. Commented May 18, 2012 at 0:26
  • and thanks for this too deceze, if there's a more efficient way of doing this than thats the way i want to go, but one step at a time eh? am glad i know that this can be acheived better with arrays. Commented May 18, 2012 at 0:56

3 Answers 3

1

Your code to get the variable name should be:

${'created_page'.$num_pages}

This is because you have to evaluate the string inside the braces before you attempt to access the variable.

Your previous code was trying to access the variables $created_page and $num_pages, and simply concatenate their values into a string.

Of course, the same goes for the page_title variable

${'page_title'.$num_pages}
Sign up to request clarification or add additional context in comments.

2 Comments

wow -so many responses so quickly. domvoyt I think you knew what I was getting at best, worked like a dream and i get it. but thanks to everyone- your answers are all helping me to get a grip on this thing.
No problem. @Aknosis had another approach that would work. Basically two different ways of using "Variable variables". But as deceze said, much better done with arrays, so good luck with your learning :)
0

you could try this:

$addit_pages == 4;

for ($num_pages=1;$num_pages<=$addit_pages ;$num_pages++) {

replaceFileContent ($dir,$created_page.strval($num_pages),"*page_title*",$page_title.strval($num_pages)); //replaceFileContent ($dir,$created_page2,"*page_title*",$page_title2); //replaceFileContent ($dir,$created_page1,"*page_title*",$page_title3); //replaceFileContent ($dir,$created_page3,"*page_title*",$page_title4);
}

the PHP strval function makes any integer into a string

Comments

0

I think what you are asking is you want the variables $created_page1; $created_page2, $created_page3 but php is probably throwing a notice that $created_page doesn't exist.

You need to use variable variables (is this what they're called?)

$addit_pages == 4;

for ($num_pages=1;$num_pages<=$addit_pages ;$num_pages++) { 
    $createdVar = 'created_page'.$num_pages;
    $titleVar = 'page_title'.$num_pages;
    replaceFileContent   ($dir,$$createdVar,"*page_title*",$$titleVar); 
}

When you use $$ this first evaluates the variable $createdVar turns that into created_page1 and then evaluates created_page1 as if you had typed in $created_page1

1 Comment

Link for reference, "Variable variables" is the correct term it seems: us2.php.net/manual/en/language.variables.variable.php

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.