max100 Posted August 29, 2006 Share Posted August 29, 2006 Hi,I am very new to PHP and wondered if someone could point me in the right direction...I have 10 variables called $title1, $title2, $title3, $title4 ... etcEach variable either contains the string 'open' or 'closed' and I wish to change all those which contain 'closed' to empty variables. I understand loops, but I'm not sure how to loop through each variable name easily. Would an array come into this?Probably a simple answer, but as I say, I'm very new to this and any help would be very much appreciated.Thank you.Max100 Quote Link to comment https://forums.phpfreaks.com/topic/18992-looping-through-variable-names/ Share on other sites More sharing options...
wildteen88 Posted August 29, 2006 Share Posted August 29, 2006 Use a for loop:[code=php:0]<?php$title1 = 'open';$title2 = 'open';$title3 = 'closed';$title4 = 'open';$title5 = 'closed';$title6 = 'open';$title7 = 'closed';$title8 = 'closed';$title9 = 'open';$title10 = 'open';for($i = 1; $i < 11; $i++){ if(${'title'.$i} == 'closed') { ${'title'.$i} = null; }}for($i = 1; $i < 11; $i++){ echo '$tile' .$i . ' = ' . ${'title'.$i} . '<br />';}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18992-looping-through-variable-names/#findComment-82087 Share on other sites More sharing options...
max100 Posted August 29, 2006 Author Share Posted August 29, 2006 Great stuff.It was the variable name which was messing me up ... wasn't sure how to append the $i onto the $link, but now I see how to use the curly braces.Many thanks, much appreciated.Max100 Quote Link to comment https://forums.phpfreaks.com/topic/18992-looping-through-variable-names/#findComment-82090 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.