Create a file called hello.php in the root directory of the web server (DOCUMENT_ROOT) and write the following into it:
Example #1 First PHP script: hello.php
"
Hello World!
" ; ?>Open this file in a browser by typing in the name of your web server and /hello.php. When developing locally, this link might be something like http://localhost/hello.php or http://127.0.0.1/hello.php, but it depends on your server settings. If everything is configured correctly, this file will be processed by PHP and the browser will display the following text:
hello world
This program is extremely simple, and you don't even need to use PHP to create such a simple page. All she does is inference hello world using PHP statement echo. Please note that the file does not have to be executable or otherwise different from other files. The server knows that this file should be processed by PHP because the file has a ".php" extension, which the server settings say such files should be passed to PHP. Think of it like a regular HTML file that is lucky enough to have a set of special tags (available to you too) that can do a lot of cool stuff.
If this example does not display anything for you or displays a download window, or if you see this entire file in text form, then most likely your web server does not have PHP support or was configured incorrectly. Ask your server administrator to enable this support. Offer him installation instructions: the Installation section of this documentation. If you are developing PHP scripts at home (locally), then also read this chapter to make sure you have everything set up correctly. Also make sure you are requesting the file from the server via the http protocol. If you just open a file from your file system, it won't be processed by PHP. If problems still persist, feel free to ask for help from one of the PHP Support Receipts.
The purpose of the example is to show the format of PHP special tags. In this example we used as the opening tag, followed by PHP commands ending with the closing tag ?> . This way you can "jump in" and "jump out" of the PHP mode anywhere in the HTML file. You can read more about this in the Basic Syntax section of the manual.
Comment: A note about newlines
Line breaks mean little in HTML, but it's a good idea to keep HTML readable by wrapping it on a new line. PHP automatically removes newlines immediately after the end tag ?> . This can be extremely useful if you are using many blocks of PHP code or including PHP files that should not output anything. At the same time, it can be confusing. You can put a space after the closing tag ?> and then the space will be output along with the newline, or you can specifically add a newline to the last echo/print call from the PHP code block.
Comment: A few words about text editors
There are many text editors and integrated development environments (IDEs) where you can create and edit PHP files. For a list of some editors, see » List of PHP editors. If you would like to recommend an editor, please visit this page and ask for an editor to be added to the list. Using an editor with syntax highlighting can be a great help to your work.
Comment: A few words about word processors
Word processors (StarOffice Writer, Microsoft Word, Abiword, etc.) are in most cases not suitable for editing PHP files. If you still want to use either of these for your test script, make sure you save the file as plain text(plain text), otherwise PHP will not be able to read and run your script.
Comment: A few words about Windows Notepad
When writing PHP scripts using the built-in Windows Notepad, you must save files with a .php extension. (Notepad will automatically add the .txt extension if you don't take the following steps.) When prompted for a filename when saving a file, type the filename in double quotes (for example, " hello.php "). In addition, you can click on the "Text Documents" drop-down menu in the save file dialog box and select the "All Files" item in it. After that, you can enter the file name without quotes.
Now that you've successfully created a working PHP script, it's time to create the most famous PHP script ever! Call a function phpinfo() and you'll see a lot of useful information about your system and setup, such as available predefined variables, loaded PHP modules, and setup options. Take some time to study this important information.
Example #2 Getting system information from PHP
We talked only about static pages, that is, those that, no matter how the user interacts with them, always remain unchanged, and in order for their content or design to change, the page owner needs to edit the code manually.
Dynamic pages and what they need
In addition to static, there are also dynamic pages. Most of them are on the Internet now. Information in them is loaded from external sources, such as a database or other files. The content and formatting of such pages may change depending on the user's actions. To edit dynamic sites, it is not necessary to interfere with their code - it is enough to change the content in a specially designed file or database, which, by the way, is also a file, only structured in a certain way.
To create dynamic websites, just HTML and CSS is not enough. It also uses programming languages, as well as databases and query languages for them. Most often, modern dynamic sites use HTML, CSS, PHP, JavaScript, SQL in their work. The first two abbreviations are already familiar to you firsthand, SQL is used to access databases, JavaScript is a client-side language whose commands are processed by the browser and are often used to show you all sorts of beauty like curtains or smoothly opening photos, but PHP is a server-side programming language , which works, among other things, with the content of the site and makes it dynamic, we will come into contact with it today.
An example of using the include command
In the previous article, I talked about the block layout of the site and cited the simplest page as an example (document index.html and the file associated with it style.css).
Now we will split the document index.html into several files, each of which will contain its own part of the page, which will help to further separate the code, improve the structure of the template and, in fact, make the page dynamic. For this purpose, we will use the PHP language, or rather, only one of its directives - the function include(), which includes one file within another.
1. Change the permission of the file created in the article about block layout index With .html on the .php to name the document index.php. File type .PHP indicates to the server that the document was written or uses inserts in the programming language of the same name.
2. In the folder with the page, create a directory blocks.
3. All auxiliary information (top, bottom, navigation and sidebar of the site) will be put into separate files, which we will place in the folder blocks.
So, create four files in the blocks directory: header.php, navigation.php, sidebar.php and footer.php. Fill in the files with code.
header (site header)
4. Check the template folder structure. The files must be in the root index.php, style.css and directory blocks.
Folder structure blocks should be like this.
5. In file index.php delete the existing code and write a new one:
Main page content
In the browser, the index.php file looks exactly the same as before, but the structure of the template has completely changed. We will talk about what happened later, and now we will answer the question about the mysterious commands of the form .
Like HTML code, PHP code also has its own start and end designation. So, you need to start a PHP insert with the command , and end with the line ?> . Between these commands, the main code is written. In our case, this is just one command - include.
Function include() inserts code from another file into the file, making it possible to store different parts of the page in different documents, thereby reliably separating them from each other.
As a result of the actions performed, we got a dynamic page index.php, parts of which are loaded from different files. Thanks to this, you can create other pages by loading auxiliary elements from the folder files in the same way. blocks.
This approach is good because if you want to change, say, the name of a menu item on a site of 20-30 pages, then in a template with a newly created structure, you will need to make changes to only one file - blocks/navigation.php, and the menu will change immediately on all pages in which it is included. If the site were static, then to change the name of one menu item, you would have to make changes to every from 20-30 pages. The difference is obvious.
In the last lesson, we figured out what blocks the trip template will consist of, so you can get started. Let's start by creating two folders:
images - this folder will contain any image files used to style the template. Because we don’t have any design developments yet, then throw any one graphic file into this folder, otherwise Joomla will not install the template and will give an error if the folder is empty.
ATTENTION: Content graphics cannot be placed in the images folder of the template!
css - this folder will contain cascading style sheet files. To begin with, let's place an empty template.css file in it, with the help of which various design styles will be assigned to the site elements.
Next, you can start creating the most important index.php file, which will determine the visual location of the site elements and tell the Joomla CMS in which block to place various components and modules. The file is a combination of PHP and HTML.
I always use only Macromedia Dreamweaver when writing code. An excellent program, I strongly advise it to beginners, because. if you make a mistake while working on the code, the program will definitely highlight your jamb.
On the site you will find a tutorial on Macromedia Dreamweaver. If you are going to develop websites, then this program should be mastered, at least at the initial level, in order to edit template codes without errors.
Positioning of elements (blocks) of the page is done using HTML code, specifically we will use DIV tags. But the way our site will work on the Joomla engine, i.e. it will be dynamic, you will also have to use the PHP language. With its help, we will determine in which blocks the positions for displaying modules will be located, and how these positions will be called, whether the blocks will collapse or not. Let's connect style sheets from external files, content language, set how the site size will change, etc.
index.php
File header
The file header consists of several parts. The first part of the PHP header code is to make sure the file is not accessed directly for security reasons.
<
?php
defined ("_JEXEC" ) or die ;
JHtml::_("behavior.framework" , true ) ;
$app = JFactory::getApplication() ;
?>
<
?php echo
"<
?"
; ?> xml version="1.0" encoding=" <
?php echo
$this-> _charset ?> "?>
DOCTYPE is a very important parameter based on which the browser decides how to display this page and how to interpret the CSS.
< ! DOCTYPE html PUBLIC "- / / W3C/ / DTD XHTML 1.0 Strict/ / EN""http: / / www.w3.org/ TR/ xhtml1/ DTD/ xhtml1-strict.dtd">
The following snippet retrieves the installed language from the global configuration.
< html xmlns= "http://www.w3.org/1999/xhtml" xml:lang=" < ?php echo $this->language; ?>" lang=" < ?php echo $this->language; ?>" dir=" < ?php echo $this-> direction; ?>" >
The following is a code snippet that includes additional information for the header, which is set in the global configuration. You can see this information by looking at the source code of any web page. In particular, these are the meta tags that you already know about.
<
head>
<
jdoc:include
type=
"head"
/
>
The following lines in the header contain links to the main Joomla CSS styles.
<
link
rel=
"stylesheet"
href=
"<
?php echo
$this-> baseurl ?> /templates/system/css/system.css" type="text /css" /
>
<
link
rel=
"stylesheet"
href=
"<
?php echo
$this-> baseurl ?> /templates/system/css/general.css" type="text /css" /
>
To use the template design styles, we make a link to the file containing the cascading style sheets template.css, which is located in the CSS folder. It does not matter that this file is still empty, the main thing is to connect it, we will deal with the design later when we install the template on Joomla. This will make it easier to see the result.
< link rel= "stylesheet" href= "< ?php echo $this-> baseurl ?> /templates/< ?php echo $this-> template ?> / css/ template.css" type="text /css" / >
The following code snippet allows us to collapse the left or right columns if there are no modules in the "left" and "right" positions. If both columns are collapsed, then the content takes up 100% of the page width. If only one column is included, then the content takes up 80%. With two columns included, content accounts for 60% of the page width.
<
?php
if ($ this-> countModules("left and right" ) == 0) $contentwidth = "100" ;
if ($ this-> countModules("left or right" ) == 1) $contentwidth = "80" ;
if ($ this-> countModules("left and right" ) == 1) $contentwidth = "60" ;
?>
Header close
<
/
head>
<
body>
The “page” block contains the design of only the page of the site, it will be the width of 950px.
< div id= "page" >
The "top" block is located at the very top of the page and contains two blocks "logo" and "user1".
< div id= "top" >
In the “logo” bokeh, we will place the logo graphic file, this will be written in the style sheets. But we write the automatic output of the site name in the index.php file, and the name is placed in the H1 tag, which is very important for search engine optimization.
<
div id=
"logo"
>
<
h1>
<
?php echo
$app
-
>getCfg("sitename") ; ?><
/
h1>
<
/
div>
Let's define the position "user1" in the block of the same name to display the site search module.
<
div id=
"user1"
>
<
jdoc:include
type=
"modules"
name=
"user1"
style=
"xhtml"
/
>
<
/
div>
<
/
div>
<
!
-
-
конец блока top -
-
>
Output of the horizontal menu module in the "user2" block at the "user2" position. The block will collapse if there is no module at that position.
<
?php if
($this-> countModules("user2") ) : ?>
<
div id=
"user2 "
>
<
jdoc:include
type=
"modules"
name=
"user2"
style=
"xhtml"
/
>
<
/
div>
<
?php endif
; ?>
Next comes the site header block "header". In it, we will define the position "header" for the output of modules. The block will collapse if there is no module at that position. I intentionally expanded the capabilities of this block in order to be able to place in it not only the header image, but also image rotators.
<
?php if
($this-> countModules(" header")
)
: ?>
<
div id=
"header">
<
jdoc:include
type=
"modules"
name=
"header" style="xhtml" />
<
/
div>
<
?php endif
; ?>
In the "user3" block, define the position "user3" for displaying modules.
The block will collapse if no module is displayed at this position "user3".
<
?php if
($this-> countModules("user3") ) : ?>
<
div id=
"user3"
>
<
jdoc:include
type=
"modules"
name=
"user3"
style=
"xhtml"
/
>
<
/
div>
<
?php endif
; ?>
The block of the left column opens, which will collapse if there is no module in the “left” position.
<
?php if
($this-> countModules("left" ) ) : ?>
<
div id=
"left"
>
<
jdoc:include
type=
"modules"
name=
"left"
style=
"xhtml"
/
>
<
/
div>
<
?php endif
; ?>
The most important block of content opens, which can take up 100% of the page width, 80% and 60%, depending on the number of columns included.
< div id= "content< ?php echo $contentwidth ; ?> " >
Displaying messages in components
< jdoc:include type= "message" / >
Content output.
<
jdoc:include
type=
"component"
style=
"xhtml"
/
>
<
/
div>
<
!
-
-
конец блока контента-
-
>
The block of the right column opens, which will be collapsed if there is no module in the “rigth” position.
<
?php if
($this-> countModules("right") ) : ?>
<
div id=
"rigth"
>
<
jdoc:include
type=
"modules"
name=
"right"
style=
"xhtml"
/
>
<
/
div>
<
?php endif
; ?>
Output of the "footer" block, designed to display the "HTML code" module with copyright information. You can also place a bottom horizontal menu or a content presentation module here. The block will collapse if more than one module is not displayed in this position of "footer"
<
?php if
($this-> countModules("footer") ) : ?>
<
div id=
"footer"
>
<
jdoc:include
type=
"modules"
name=
"footer"
style=
"xhtml"
/
>
<
/
div>
<
?php endif
; ?>
The site page block "page", body and all code are closed.
<
/
div>
<
!
-
-
конец блока page-
-
>
<
/
body>
<
!
-
-
конец блока body -
-
>
<
/
html>
<
!
-
-
конец кода-
-
>
We have created a complete index.php file. Now you know with what commands and in what order the template blocks are displayed.
ATTENTION: In order for the template code to be read from the joomla admin panel, the index.php file must be opened in the AkelPad editor and saved in UTF-8 encoding, while unchecking BOM. If you used the Macromedia Dreamweaver program to work with the file, then you need to select "Edit" > "Page Properties" in the return menu and select the Unicode document encoding (utf-8), while unchecking "enable Unicode signatures (BOM)". However, I strongly advise you not to edit the code from the Joomla admin panel, if you screw up something - there is no turning back, unlike the Macromedia Dreamweaver program, where you can always undo the changes made.
The very design of the blocks will be described in template.css. But we will customize the style sheets after installing the template on Joomla 3 (joomla 2.5), and for this you need to create
So, friends, if you have reached this lesson, then you have managed to either install a local server or buy a hosting where you can work with PHP. Congratulations - that's a big step!
I will say briefly about PHP - this programming language is used all over the world and you can create sites of all levels of complexity on it, from business card sites to large portals. I think it is not a secret for many that the largest social networks facebook.com(from scratch in php) and vk.com(engine in php) were written in PHP. So let's draw conclusions and start working!)
How the code works
PHP code processed on the server side. That is, there is no finished page. For example, in the code the command is given to collect data on how many users are currently registered on the site. Website visitor clicks on a link All users. He wants to get dynamic data, that is, those that are constantly changing. After the calculation on the server is completed, data will come from the server in the form of a generated HTML code of the page with the number of users. As a result, after clicking-request on the link, the user receives the page. If you view the code of the resulting page, you can see only HTML, and the PHP code will not be available for viewing. Roughly speaking, PHP is instructions to the server on how and from which blocks to make a page.What does PHP code look like and where should I paste it?
PHP code can be embedded directly into HTML. PHP code is embedded in HTML pages using angle brackets and a question mark , however, you can restrict yourself to brackets with question marks код; ?> . You will only need to change the file extension, for example from .html on the .phpPHP code(file index.php)
echo "Hello world!";
?>