How to Center in PHP

Techwalla may earn compensation through affiliate links in this story.
PHP can print HTML position tags directly to the Web browser.

The PHP language is a Web programming language designed to take full advantage of processing power and memory on the server to perform calculations, HTML (Hyper-Text Markup Language) code display, and database interaction. PHP was designed to permit the inclusion of HTML presentation commands, known as tags, directly into its own echo commands. The echo command allows PHP to print straight text or HTML information to a visitor's Web browser screen. Through the use of echo along with the HTML

tag, text and images may be centered on the browser.


Step 1

Set up your text editing software to permit entering and saving a straight text file. If you are using Notepad on Microsoft Windows, no configuration is necessary as it works with straight text by default. If you choose to use an Apple Mac system's "TextEdit," the program must be set correctly for use with this article. Start TextEdit. Select "Preferences" from the "Application" menu, then click the "Plain Text" radio selector for the "New Document Format" category. In the "Saving" section of the configuration screen, click the checkbox to turn off "Append '.txt' extension to plain text files." Under "Rich Text" click the checkbox to select (turn on) the option labeled "Ignore rich text commands in HTML files." Turn off wrapping by clicking on the checkbox to deselect "Wrap to Page" to complete TextEdit setup. Save the changes and open a new document. If using Notepad, open it now. It is under the "All Programs\Accessories" menu when clicking on the Windows start button.


Step 2

Create a new HTML file into which the PHP command will be entered. A basic HTML file must include the following, in order: My Page Title Here

Step 3

Set up the location where the PHP command will go. Every PHP command begins with an opening tag and a closing tag. The opening tag can be either If placed on one line, a complete open and close would appear as follows: Place this between the and the tags of the HTML document.

Step 4

Enter a line of PHP between the open and closing tags that prints a large bold heading at the top of the browser page and centered. To do so, you must use the PHP command "echo", then a quotation mark, followed by the HTML and text information, then finished with a closing quotation mark and a semicolon. An example that creates a large bold header centered at the top of the browser screen that reads "My First Centered PHP Header" (including opening and closing program tags) would appear as such:

My First Centered PHP Header

"; ?> If you are unfamiliar with HTML, the

tag turns on large bold header and

turns off large bold header.


Step 5

Add more information to the PHP code chunk if desired. If you would like to have an image centered below the header of the page, and you have the image named "mycat.jpg" stored in an "images" directory that resides in the same directory into which the HTML file containing the PHP code will be stored, it may be added so the complete PHP code chunk appears as follows:

My First Centered PHP Header

"; ?>

Step 6

Save the entire file to your Web server directory so it will be able to process it when visitor loads it into the browser. Save it with any name desired, but be certain to save it with a .php extension at the end of the name. An example would be: headerwithimage.php


Step 7

Point your Web browser to the file to view the processed result. If your web address is and the file was saved in the top directory (the one containing the "images" directory as in this example) then you would enter: Providing no typing mistakes were made, the header will appear centered at the top of the page, and the image will appear directly below the header, also centered.


Always be sure to end each PHP echo line with a semicolon, as this is required for the PHP processor to know when a line ends before moving on to the next.