0

How do I write this out properly?

$loccity=array("Atlanta","Boston");

foreach ($loccity as $city) {
   $myurl = 'http://$city.mysite.com';
   echo $myurl;
}
  1. Can I just stick $city in the middle of $myurl like that?
  2. Will using a foreach loop like this work in this way or is a counter needed?
7
  • 1
    Voting to close. You can test that yourself and you will know! Not a real question! Commented Aug 15, 2011 at 17:11
  • @markus: It is a real question, even though the answer is easily deducible. Commented Aug 15, 2011 at 17:13
  • Variable interpolation works but only with double quotes, a foreach loop is the perfect choice. Even better then interpolation (smelly) would be to use sprintf. Commented Aug 15, 2011 at 17:13
  • @markus: That might be better off as an answer, not a comment. Commented Aug 15, 2011 at 17:13
  • @Tomalak: It may be a real question... if yes, then it's a xlicate and still has to be closed. It's also a RTM question IMO. Commented Aug 15, 2011 at 17:14

4 Answers 4

3

2. Will using a foreach loop like this work in this way or is a counter needed?

Yes, it'll work properly.

1. Can I just stick $city in the middle of $myurl like that?

Almost.

You forgot that variable interpolation doesn't work with single quotes, but with double quotes:

<?php
$loccity = array("Atlanta", "Boston");

foreach ($loccity as $city) {
   $myurl = "http://$city.mysite.com";
   echo $myurl;
}

// Output: http://Atlanta.mysite.comhttp://Boston.mysite.com
?>

Live demo.


You may also want a newline between items:

<?php
$loccity = array("Atlanta", "Boston");

foreach ($loccity as $city) {
   echo "http://$city.mysite.com\n";
}

// Output:
// http://Atlanta.mysite.com
// http://Boston.mysite.com
?>

Live demo.

Sign up to request clarification or add additional context in comments.

4 Comments

You were right.. the double quotes not being there was causing my problem. But it wasn't because I forgot. I didn't know! :) Thanks so much. I really appreciate it!
@Andi: Not a problem. FWIW, this is all covered in the [admittedly poorly structured] documentation .. which of course you've read several times I hope?! :)
I go there all the time. Unfortunately, I am a terrible programmer.. which bugs the crap out of me because I love this stuff. When I try to apply something I read to my own example, I seem to usually fail. :( But.. I will keep trying. :)
@Andi: You're certainly on the way there.
0

Replace your single quotes with double quotes.

For good measure, wrap your '$city' in curly brackets. So "http://{$city}.mysite.com/"

Comments

0

Why wouldn't it work? Just use double quotes when using $variable inside text

http://sandbox.phpcode.eu/g/466fd.php

<?php
$loccity=array("Atlanta","Boston");

foreach ($loccity as $city) {
    $myurl = "http://$city.mysite.com<br>";
    echo $myurl;
}

4 Comments

Thank you! This was my problem and why I asked the question. People above were jumping on me that I could easily test. I did and was getting an error that it didn't work. I didn't realize I had to wrap it in double quotes if using a variable inside.
If I helped you, please help me: when possible, accept my answer (click on that link if you're not sure how to do so)
@genesis: "newbie" has already accepted answers to five questions before this one!
@TomalakGeret'kal: ... I just wanted to say it's not begging. I place these comments only when there is comment from OP like "Thanks"
0
<?php
 $loccity=array("Atlanta","Boston");

 foreach ($loccity as $city) {
 $myurl = 'http://'.$city.'.mysite.com';
 echo $myurl;
 }
 ?>

. operator is concatenation operator.
output
http://Atlanta.mysite.comhttp://Boston.mysite.com

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.