Solved

What does the "<<<" do in PHP syntax?

Posted on 2010-09-01
7
315 Views
Last Modified: 2012-05-10
What does the "<<<" do in PHP syntax?  Why is it used?

And, yes, I did try 'Googling' and searching here in EE, but of course these special characters are completely ignored in searches, even with quotes around it --- or as a Side Question, if that's a better way to answer, how would one search in EE or Google for special characters?  I have several web resources, but am often stumped on searching special characters that I see in other's code, like "===" (which I finally figured out means "equal in value and type").

Thanks,
murach

Example Code: 
function myHeader()
{
  $myVariable = <<<EOFORM
     <html>
     <head>
     <title>Test Title .....
     ....
     </head>
   EOFORM;
   return($myVariable);
}

Open in new window

0
Comment
Question by:murach
7 Comments
 

Expert Comment

by:Korcan
ID: 33579716
Nothing.This code wrong and <<< is wrong for php.Php doesnt have <<< in operators or php syntax.

Best Regards
0
 
LVL 16

Expert Comment

by:carsRST
ID: 33579735
Appears to be sample code, indicating your data will be replaced by the "<<<EOFORM"

0
 
LVL 4

Expert Comment

by:mpickreign
ID: 33579838
The code is correct. It opens a text stream that can cover multiple lines. For example (everything from <<<XML to XML will be stored in the var.

$string = <<<XML
<?xml version='1.0'?>
<document>
 <title>Forty What?</title>
 <from>Joe</from>
 <to>Jane</to>
 <body>
  I know that's the answer -- but what's the question?
 </body>
</document>
XML;
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:murach
ID: 33579871
I'm seeing several versions of the "<<<" on different websites (and also in our company's own "working" code.... so it wasn't 'sample' code, though granted, my example above was 'sample-ized').  
Perhaps it is a way to avoid using a Quotes?  I'll  had thought that if I found the "manual" for using that set of symbols, then I  could be on my own way to deciphering other shortcuts in the future.


    $query = <<<EOQUERY
INSERT INTO Appointments
  SET
    user_id = $in_userid,
    title = '$title',
    start_time = '$start',
    end_time = '$end',
    location = '$location',
    description = '$desc'
EOQUERY;

....

echo <<<EOHEADER
<h2 align='center'>Create New Appointment</h2>
$msg
EOHEADER;

Open in new window

0
 
LVL 4

Accepted Solution

by:
mpickreign earned 500 total points
ID: 33579922
0
 

Author Closing Comment

by:murach
ID: 33580022
Looks like "40" IS the answer, glad I asked the Question here.  THANK You, PHP expert, "mpickreign"!

NOTE to anyone who may plan to use "<<<":  Read the the details that are in the link from PHP that  "mpickreign" sent above, as there are Warnings and notes about it (placement in the first column, no other spaces/characters, etc.)

"........way to delimit strings is the heredoc syntax: <<<.    After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.  
..........."

0
 

Author Comment

by:murach
ID: 37109487
btw, Just adding a follow-up note as to where the "<<<" is named "heredoc" syntax:  it originates for Unix Syntax.

I was reading some Unix-scripting reference material and it gives the following description:
".....Some Unix shells support the concept of a "here-document," which serves essentially the same purpose as file input to a command, except that no explicit filename is involved. (In other words, the document is located "right here" in the script, not stored in an external file.) To provide input to a Unix command using a here-document, use the following syntax:
   Unix-command <<MARKER
   input line 1
   input line 2
   input line 3
   ...
   MARKER

The <<MARKER signals the beginning of the input and indicates the marker symbol to look for at the end of the input.
"

Thanks again, Experts-Exchange.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now