This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Introduction to PHP

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Week 2 assignment

Go back to: General discussion

Here's my codepad of the last question. 

http://codepad.org/TygoR2G5

How do we get apostrophes in variable names?

For the creation of the last portion...the url, I put the 'dot' of the dot org and of the  'www.'  inside the variables. Would like to see how others are handling the url creation.

Aryeri Kai's picture
Aryeri Kai
Mon, 2011-01-31 04:40

One way could be http://codepad.org/FlOJFRpe

Nathan Wrigley's picture
Nathan Wrigley
Mon, 2011-01-31 22:27

Hi there,

This is how I went about it:

http://codepad.org/xqLmNQR5

basically the same as Aryeri although I have a different URL!

Does anyone know if one of these is 'more' correct:

echo "http://" .$url4 ."." . $url3 ."." . $url1 . "/". $url2; OR
echo "http://" . $url4 . "." . $url3 . "." . $url1 . "/p2pu-" . $url2;

in terms of the way that spaces have been put between the concatenated '.'. Does a space in fact matter?

Fergus Gibson's picture
Fergus Gibson
Tue, 2011-02-01 20:44

You can see a number of examples here: http://codepad.org/4Fs9bPje

The short answer is simply that, no, the PHP parser doesn't care if you put one, two, or no spaces around the concatenation operator. But at the same time, it does matter. If you look at the example link I pasted above, you can see that the line with spaces is easier to read. And that's why most programmers put spaces around all their operators.

Nathan Wrigley's picture
Nathan Wrigley
Tue, 2011-02-01 21:06

echo '$chunk1 $chunk2' . PHP_EOL;

I think that the above does NOT work due to the single quotes - code is not executed in the single quotes?

---
printf('%s %s' . PHP_EOL, $chunk1, $chunk2);

This one has me baffled! I'm guessing that the %s (1st) is related to $chunk1 and the 2nd %s to $chunk2 - but why... I don't know.

Thanks for the heads-up with PHP_EOL too - that's nice. I'm guessing that I will be using Linux 99.9% of the time, but it's always good to know.

Fergus Gibson's picture
Fergus Gibson
Wed, 2011-02-02 11:44

Spot on, Nathan. The difference between single and double quotes is that in single quotes, the PHP parser doesn't interpret any of the contents and just presents them literally. This includes not translating variables into their values, and it also means that escaped characters (like \t for tag, \n for line-feed) are not translated either. Single-quotes are good when you want the string to be totally literally with no substitution.

Sorry that printf() was baffling. I was just sharing a completely different way -- "for fun", as I wrote -- but since I brought it up, yes, you're basically right. printf() uses substitution placeholders in its format string. %s means to render a variable as a string (as opposed to as an integer, float, or some other type of value). It maps the placeholders in order to the variable arguments. In this case, then, the first %s is replaced by $chunk1 formatted as a string, and the second %s is replaced by $chunk2. You can actually get quite fancy with the placeholders, that was a very basic example. I just threw it in for fun, but great job tackling it and you intuited well.

As for using Linux almost exclusively, keep in mind that if you contribute or share your code, it may find its way onto a Windows computer. I recommend using PHP_EOL simply because it's a good habit to get into. It'll never be wrong, and it can be very helpful in cases where you might not have expected the code could eventually end up on Windows and does. Or vice versa for the Windows crowd who don't expect to run their code on Linux, a Unix (which includes Mac OS X).

intro vert's picture
intro vert
Tue, 2011-02-01 02:10

According to this http://php-faq.com/#110 "Inside double quotes variables will be converted into their values"

<?php
 
echo "http://$url4.$url3.$url1/p2pu-webcraft";
?>

which is a lot easier for me to read and less to type.

Nathan Wrigley's picture
Nathan Wrigley
Tue, 2011-02-01 20:59

Your method is MUCH more elegant than mine - I will try to remember that from now on.

Thanks.

Son Tran's picture
Son Tran
Wed, 2011-02-02 00:58

Ah, that's great!

Tamara's picture
Tamara
Wed, 2011-02-02 18:21

Please help me with the third part of the assignment. I don't understand the question. By typing "echo $c*2;" I'm not modifying the values, nor creating new variables, but it can't be the answer. I don't know what to do((
Thanks

Maya Incaand's picture
Maya Incaand
Wed, 2011-02-02 18:36

You need to combine the variables using mathematical operators to produce the desired answers.

Tamara's picture
Tamara
Wed, 2011-02-02 19:29

Oh, got it.
Thank you!

Fergus Gibson's picture
Fergus Gibson
Wed, 2011-02-02 23:02

Good job helping each other out! That's exactly what we want to see. :)

Tamara's picture
Tamara
Fri, 2011-02-04 17:53

Hi all!
Here's my codepad for the 4th question.
http://codepad.org/xDqYWuUP
Please tell me what I'm doing wrong.