/ / Introduction
The where, what and how
- Once you are connected to the internet, open the Fetchapplication
- Connect to the remote server
- Login to the remote server


- Navigate to your folder within the cgi-bin folder by double clicking on them.
- Use BBEdit
to write your text files.
- Login to the remote server
- Save your files with the .php extension
- To access your php page through the browser, follow this path:
http:pcomp.lizarum.com/cgi-bin/your_first_name/your_file.php
/ / A Structured Document
To create a PHP page start with a text editor.
To properly structure a page, begin and end the document with:
<?php ?>
php Extention
If you have PHP inserted into your HTML and want the web browser to interpret it correctly, then you must save the file with a .php extension. Instead of index.html, it should be index.php if there is PHP code in the file.
Semicolons
Every statement in PHP ends with a semicolon.
Comments
PHP support three kinds of comment tags :
- //
This is a one line comment -
#
This is a Unix shell-style comment. It's also a one line comment -
/* ..... */
Use this multi line comment if you need to.
/ / Variables
A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. After varibles are initialized they can be reused throughout your code. In PHP you define a variable with the following form:
$variable_name = Value;
There are a few rules that you need to follow when choosing a name for your PHP variables.
- PHP variables must start with a letter or underscore "_".
- PHP variables may only be comprised of alpha-numeric characters & underscores. a-z, A-Z, 0-9, or _ .
- Variables with more than one word should be separated with underscores. $my_variable
- Variables with more than one word can also be distinguished with capitalization. $myVariable
echo
The PHP function echo is a means of outputting text to the web browser.
echo "Hello world";
String Creation Heredoc
$my_string=<<<MY_STRING Saint Anns School<br /> 129 Pierrepont Street<br /> Brooklyn, NY 11201 MY_STRING;
- Use <<< and some identifier that you choose to begin the heredoc. In this example MY_STRING is the identifier.
- Repeat the identifier followed by a semicolon to end the heredoc string creation. In this example that was MY_STRING;
- The closing sequence MY_STRING; must occur on a line by itself and cannot be indented!
Another thing to note is that when you output this multi-line string to a web page, it will NOT span multiple lines. To insert a carriage RETURN you need to use the <br /> tags contained inside the string. Here is the output made from the code above:
/ /Calling a PHP page
-
Open BBEdit and create a new HTML document by pressing Command+ CTRL+N
- Between the body tags insert a pair of div tags
- Within the div tags
insert the following link (modify your code where appropriate)
The simplest method you can use to send information along with a page request uses the URL query string. If you've ever seen a URL in which a question mark followed the file name, you've seen this technique in use.
<form action="your_path/welcome1.php" method="get"> <label> What is your name?<input type="text" value="" name="name" length="20" /> </label><br /> <input type="submit" value="click here" /> </form>
You linked to a file called welcome1.php, but you also passed a variable along with the page request. The variable was passed as part of the query string, which is the portion of the URL that follows the question mark. The variable is called name and its value is your name. - Save the file as index.html in your web folder in pcomp on the remote server.
- Create a new HTML file, but, this time, save the file as
welcome1.php and save it in your folder inside cgi-bin.
- Add a pair of <div></div> tags inside the <body></body> element
- Inside the add this php script:
<?php $name=$_GET['name']; echo "Welcome to my page, $name!"; ?>
PHP creates an array variable called $_GET that contains any values passed in the query string. $_GET is an associative array, so the value of the name variable passed in the query string can be accessed as $_GET['name']. Your script assigns this value to an ordinary PHP variable ($name), then displays it as part of a text string using an echo statement.
You can pass more than one value in the query string.
Here I create an anchor link with 2 variables:
<a href="welcome2.php?firstname=Liz&lastname=Arum">It's me!</a>
<form action="your_path/welcome1.php" method="get">
<label>
What is your first name?<input type="text" value="" name="firstname" length="20" />
</label><br />
<label>
What is your last name?<input type="text" value="" name="lastname" length="20" />
</label><br />
<input type="submit" value="click here" />
</form>
<?php $firstname=$_GET['firstname']; $lastname=$_GET['lastname']; $name=$firstname." ".$lastname; echo "Welcome to my page, $name!"; ?>
<a href="welcome2.php?firstname=Liz&lastname=Arum">It's me!</a><form action="your_path/welcome1.php" method="post"> <label> What is your first name?<input type="text" value="" name="firstname" length="20" /> </label><br /> <label> What is your last name?<input type="text" value="" name="lastname" length="20" /> </label><br /> <input type="submit" value="click here" /> </form>
<?php $firstname=$POST['firstname']; $lastname=$POST['lastname']; $name=$firstname." ".$lastname; echo "Welcome to my page, $name!"; ?>
Using post and a form send at least two fields to another page that incorporates the sent data.