Your first PHP-enabled page
Create a file named hello.php and put it in your web server’s root directory ( DOCUMENT_ROOT ) with the following content:
Example #1 Our first PHP script: hello.php
Use your browser to access the file with your web server’s URL, ending with the /hello.php file reference. When developing locally this URL will be something like http://localhost/hello.php or http://127.0.0.1/hello.php but this depends on the web server’s configuration. If everything is configured correctly, this file will be parsed by PHP and the following output will be sent to your browser:
This program is extremely simple and you really did not need to use PHP to create a page like this. All it does is display: Hello World using the PHP echo statement. Note that the file does not need to be executable or special in any way. The server finds out that this file needs to be interpreted by PHP because you used the «.php» extension, which the server is configured to pass on to PHP. Think of this as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things.
If you tried this example and it did not output anything, it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled, or is not configured properly. Ask your administrator to enable it for you using the Installation chapter of the manual. If you are developing locally, also read the installation chapter to make sure everything is configured properly. Make sure that you access the file via http with the server providing you the output. If you just call up the file from your file system, then it will not be parsed by PHP. If the problems persist anyway, do not hesitate to use one of the many » PHP support options.
The point of the example is to show the special PHP tag format. In this example we used . You may jump in and out of PHP mode in an HTML file like this anywhere you want. For more details, read the manual section on the basic PHP syntax.
Note: A Note on Line Feeds
Line feeds have little meaning in HTML, however it is still a good idea to make your HTML look nice and clean by putting line feeds in. A linefeed that follows immediately after a closing ?> will be removed by PHP. This can be extremely useful when you are putting in many blocks of PHP or include files containing PHP that aren’t supposed to output anything. At the same time it can be a bit confusing. You can put a space after the closing ?> to force a space and a line feed to be output, or you can put an explicit line feed in the last echo/print from within your PHP block.
Note: A Note on Text Editors
There are many text editors and Integrated Development Environments (IDEs) that you can use to create, edit and manage PHP files. A partial list of these tools is maintained at » PHP Editors List. If you wish to recommend an editor, please visit the above page and ask the page maintainer to add the editor to the list. Having an editor with syntax highlighting can be helpful.
Note: A Note on Word Processors
Word processors such as StarOffice Writer, Microsoft Word and Abiword are not optimal for editing PHP files. If you wish to use one for this test script, you must ensure that you save the file as plain text or PHP will not be able to read and execute the script.
Now that you have successfully created a working PHP script, it is time to create the most famous PHP script! Make a call to the phpinfo() function and you will see a lot of useful information about your system and setup such as available predefined variables, loaded PHP modules, and configuration settings. Take some time and review this important information.
Example #2 Get system information from PHP
Create .html file with php
i want to create a new .html file using php script as i new to php i don’t understand to do it. i founded a code but it m not working.
i want that when i load my page, an new .html file should be created to my path/ directory with some entries like heading e.t.c in my html file can anyone help me??
You clearly don’t understand at all what you’re doing, try reading some tutorials to at least grasp the necessary basics. Your question and your code are not related in any way.
3 Answers 3
Best way to do it would be to write a small blank html file and save it somewhere; e.g. «template.html»
1) in PHP you can read it with
$newcontent = file_get_contents("template.html");
2) the open a new file with fopen, write new content, close the file. Done.
if (!file_exists('newname.html')) < $handle = fopen('path/to/new/file/newname.html','w+'); fwrite($handle,$newcontent); fclose($handle); >
So, there are two parts of creation of new file:
First you need to create content of that file. Here you can combine HTML and PHP code. For example
Or use nay template engine or code generator — and generated code save into any variable that you will use as content of created file.
And then you may create own file:
`$Filename` means name of generated file (including its path) `$Mode` means mode of file generation
Both arguments may be passed also directly (for example fopen(‘file.ext’, ‘a’) )
Writing content of file follows
$Status = fwrite($File, $TagCode);
And that is all — unless you would control if file was created correctly (then you use that function fwrite returns TRUE or FALSE — that you store in any variable — it is better than direct checking).
There are following modes of opening of files
- a — file content is not overwritten; only for adding of content; new content is placed below older content
- a+ — file content is not overwritten; for adding/reading of content; new content is placed below older content
- r — file content may be only read
- r+ — file content is not overwritten;, for adding/reading of content; new content is placed above older content
- w — file content is overwritten, only for adding of content
- w+ — file content is overwritten; for adding/reading of content
Instead functions fopen , fwrite and fclose may be used also file_puts_content , but I like fopen, fwrite and fclose more.
create and save pure html file from php ‘template’?
I am looking for a simple and effective way to create a pure html file based off a php file. For instance, in template.php below the php would be inserting various portions of the page. I want to save the page then as html removing all php code and leaving what was inserted by it. hopefully that makes sense. the output of template.php would be a better way to say it I guess. First, I do not know if something like this is possible. Second, is this the best way to go about something like this? Before anyone starts screaming about security there will be ZERO user submitted / form submitted variables in this page. My goal is to create a report from database values with the template which the user can then view/print/save off the server as pure html. There will be no images only inline css. EDIT : This html only output of template.php needs to be saved on the server as its own file. The reason for the php ‘template’ is because I will be creating the vast majority of the page with php. but I only want to save its resulting output. template.php :
" name="description" /> . further html with php mixed in
Current solution : I did some further research and this is acting exactly how I want it to. Comments/suggestions welcome for it.
Most browsers let you save a page to an html file. Could you just view the page in your browser, right click and do ‘save as’?
You will find everything you need on PHP.net’s filesystem directory php.net/manual/en/ref.filesystem.php