Php punctuation

I find it hard to write PHP .and "straight when recording to a file in the script. I'm new, so this looks confusing. The book does this:

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

The question is what is the appropriate placement for periods and quotes. Because of how it all came together, I don’t know what they should be attached to. Should each variable be there ".$VARIABLE."or is it for tabs such as \t". I want to reorder it, so there is a line segment there, followed by a variable, then a new line. For example:

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

Will this work? I do not have a php server on the machine I am testing on. I hope this changes a little, basically I'm not sure what all the punctuation is for. Thanks.

+4
source share
8 answers

dot (.) is the php concatenation operator. In English, this means that it will glue strings together.

static lines are defined as "quoted" text

in PHP, the dynamic typing of all scalar (numbers, strings) variables can be used as strings

So...

// 3 static strings 
echo "hello" . " " . "world"; // prints 'hello world'

// 2 variables
$a = "hello";
$b = "world";
echo $a . $b ; // prints 'helloworld';  (no space)

// mixed variables and static string
echo $a . " " . $b; // prints 'hello world'

Additionally, you should be aware that there is a difference between single quotes (') and double quotes (")

single quotes are taken literally.

echo '$a $b'; // prints '$a $b' with literal $
echo "$a $b"; // prints 'hello world' where $a and $b are treated as variables
+2
source

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

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

Here are all good answers to help you understand what is going on, to find out more about this, you can check the php man page:

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

+5

PHP- (") :

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

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

- var - foo, var2 - bar

, (.):

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

- var - foo, var2 - bar

+5

, PHP . - ' ",

,

$name = 'Sam';
$some_variable = 'THIS IS STRING'[seprator]' OTHER_STRING '[seprator]$name;

$some_variable = 'THIS IS STRING'.' OTHER_STRING '.$name;

$some_variable = 'THIS IS STRING OTHER_STRING Sam';
+5

. php 2 .

:

echo "Hi" . " " . "there";

:

Hi there

" - , , " , , blackslash \

:

echo "This \"IS\" the best!";

:

This "IS" the best!

, , , , .

+4

- !

$outputstring = "$ date\t $tireqtytires\t $oilqty oil\t $sparkkty \t\$$ totalamount\t $address\n";

+3

. .

, , . . html , \n.

+2

. , . / .

Periods essentially add each individual component (variable or line) together to form one long line. Some other languages ​​are used +instead ., which may be more intuitive.

+1
source

Source: https://habr.com/ru/post/1527990/


All Articles