Is there any line continuation character (& _) available in PHP?

19,824

Solution 1

In PHP, once you don't close the quotation, you can write your code on multiple lines. Example:

$sql = "select stoks,srate,prate,taxp,
        iname,suplier,icod from stock where iname='".$item_name."'
        AND  suplier ='".$suplier." '";
mysql_query($sql);

Solution 2

PHP is different from the various versions of VB in that it uses a line termination character. (http://php.net/manual/en/language.basic-syntax.instruction-separation.php) As such there is no line continuation character required. Your long strings can just continue from line to line with no need to close the quotation.

$sql = "select stoks,srate,prate,taxp,
        iname,suplier,icod from stock where iname='".$item_name."'
        AND  suplier ='".$suplier." '";

Or you can close the quotation block and connect it to another one using the period concatenation operator "."

$sql = "select stoks,srate,prate,taxp," .
       "iname,suplier,icod from stock where iname='" . $item_name . "'" .
       " AND suplier ='" . $suplier . " '";

A third way to accomplish the same goal is to use the concatenation assignment operator. (http://php.net/manual/en/language.operators.string.php)

$sql  = "select stoks,srate,prate,taxp,";
$sql .= "iname,suplier,icod from stock where iname='".$item_name."'";
$sql .= " AND  suplier ='".$suplier." '";

There are other ways to accomplish the same idea, but these seem to be the most popular.

Solution 3

PHP doesn't require it, but you can also concatenate strings in a similar fashion:

$myString = "Hello my name is john I am a super cool dude that likes cheese" .
"I also like milk" .
"I also like the number 8";

Solution 4

Current answers explain how to continue a concatenation. But what about any other expression, like a + b + c + ... ;?

You can use parenthesis to achieve expression continuation (which includes the concatenation). So, you can write expressions like:

# A chain of conditions
$apply = (
       $condition1
    && $condition2
    && $conditionn);

# your example sql expression
$sql = (
      "select stoks,srate,prate,taxp,"
    . "iname,suplier,icod from stock where iname='"
    . item_name.Text
    . "'"
    . "and  suplier ='"
    . suplier.Text
    . " '");
Share:
19,824
Admin
Author by

Admin

Updated on September 15, 2022

Comments

  • Admin
    Admin over 1 year

    I am dealing with long queries in PHP, so that I need to write it on multiple lines, but I don't know if their any line continuation character is available in PHP.

    I used the same in VB.NET as furnished below:

    sql = "SELECT stoks,srate,prate,taxp," & _
    "iname,suplier,icod FROM stock where iname='" & item_name.Text & "'" & _
    "and  suplier ='" & suplier.Text & " '"
    

    Is there any similar operation available in PHP, for denoting line continuation?

  • alexw
    alexw about 8 years
    Please, please please, stop promoting the use of the mysql_* library, even if it is only for illustrative purposes. mysql_ is so, so, so deprecated.
  • David A. Gray
    David A. Gray over 6 years
    So PHP behaves in that regard like C, Perl, etc. right?
  • hbogert
    hbogert about 6 years
    This gives you a lot of unneeded whitespace in the string right? Admittedly, not very important in the case of the above SQL.