Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Punctuation Confusion

Tags:

php

I'm having a hard time getting PHP . and " straight when doing a write to file in a script. I'm new, so it looks confusing. The book does it:

$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil \t".$sparkqty." spark plugs\t\$".$totalamount."\t". $address."\n";

The question is what's the appropriate placing for the periods and quotations. Due to how it's all mashed together, I don't know what they need to be attached to. Does each variable need to be ".$VARIABLE." or are they for the tabs like \t". I want to rearrange it, so there's a segment of string, followed by a variable, then a new line. What I think it should look like is:

$outputstring = $date."\n\ Tires: ".$tireqty."\n\ Oil: ".$oilqty."\n\ Spark Plugs: ".$sparkqty."\n\$".$totalamount."\n".address."\n";

Would that even work? I don't have a php server on the machine I'm at to test. I hope this makes a little sense, basically I'm not sure what all the punctuation is for. Thanks.

like image 476
user3066571 Avatar asked Nov 28 '22 21:11

user3066571


2 Answers

the . is the concatenation operator in this scenario. So basically the . stitches together two strings, something like this:

$string = "Hello";
$newString = $string . " World"; // <-- this var now contains "Hello World"

These are all good answers here that should help you understand what's going on, to read more about it you can check out the php manual page:

http://www.php.net/manual/en/language.operators.string.php

like image 125
A.O. Avatar answered Dec 25 '22 11:12

A.O.


All php variables inside double quotes(") gets interpreted:

$var  = 'foo';
$var2 = 'bar';

echo "The var content is $var and var2 is $var2";

-- The var content is foo and var2 is bar

If you want to concatenate values you need to do it with dot(.):

echo 'The var content is'.$var.' and var2 is'.$var2;

-- The var content is foo and var2 is bar

like image 27
Ed Capetti Avatar answered Dec 25 '22 13:12

Ed Capetti