Basic Function
Before i get started here i will assume you have a local server installed on your computer.
If you don’t have this already than i suggest you go to wampserver.com to download.
Wampserver comes with php and apache (the server).
Once installed navigate to where you installed it (eg. c:\wamp\)
Inside this folder is a www folder, this is where you need to create all your projects.
Inside this folder create a folder named whatever you want, i will call mine phptutorials, this is what the structure will look like:
- c:\
- wamp
- www
- phptutorials
- www
- wamp
Make sure you have wampserver started. you can navigate to http://localhost/ to make sure your server is running.
Ok so now with this said, we will start with a basic function.
- Open any text editor, I use notepad ++ but you can use Windows Notepad for this.
- Start your open and closing php tags
<?php
//php code should be between these tags.
?>
- In between the php tags type in the following function
function Message(){
echo 'Hello World';
}
The way i look at a function is like a box that can store all sorts of information and data.
Inside functions you can store variables which are also like small containers that can store information.
If you were to save and view this file in a browser you will get a blank screen, this is because we only created the box (function)
To call everything inside this function type in the following outside the function, after the curly brace }
Message();
Message is the name of the function and can be named whatever you want.
You may ask “how does it echo Hello World without having to echo the function”
When we call the function, the code inside the function executes, and because we have echo ‘Hello World’ that executes.
Full code
function Message(){
echo 'Hello World';
}
Message();
Leave a Reply
You must be logged in to post a comment.
