PHP INTRODUCTION

Basic PHP Concepts
PHP borrowed its primary syntax from C++ and C
Many of the programming techniques you've previously learned will work in PHP (assignments, comparisons, loops, procedures) with little to no syntax difference
There are, however, major changes in how data is manipulated in relationship to C/C++
C/C++ are type-specific languages, requiring the user to define a specific, singular type for each variable that they use
PHP commonly assigns its variables "by value", meaning a variable takes on the value of the source variable (expression) and is assigned to the destination variable. A variable can therefore change its type "on the fly". Therefore variables are not declared (as they are in most type-specific languages like C++)
PHP is an interpreted language, in that the PHP interpreter program reads the PHP source code, translates the code and executes it at the same time. With C++ on the other hand, the C++ compiler translates your C++ code into a binary executable, eliminating the translation of the source each time the code executes
Initially this interpreted nature of PHP sounds like a disadvantage; on the contrary, the interpreted nature of PHP provides some very intereting and useful programming techniques that are not possible in compiled languages.

Using PHP in a Webpage
PHP source code is embedded in an HTML-based document, and is identified by special delimiting tags,
<?php   content   ?>
similar to Javascript and Java applets.
<H2>My Webpage</H2>
This is my webpage.

<?php
  echo "This is written in PHP.\n";
?>
How this will appear in a browser:

My Webpage

This is my webpage. This is written in PHP.
You can switch between HTML and PHP as many times as you like within a document:
HTML content

<?php PHP content ?>

HTML content

<?php PHP content ?>

HTML content

Webpage Setup Using PHP
Two Approaches:
  • Using .php filename extension on source file.

  • Including PHP script call inside source file along with naming
    the source file with .cgi extension and making source file
    executable (UNIX environment).

Approach One:
Name your source file with a .php extension:
sample.php
index.php
(This requires proper setup on server so it understands
what to do with files with this extension.)


Approach Two:
Including call to PHP script inside source file:
Source file: sample.cgi
#!/usr/local/bin/php

<H2>My Webpage</H2>
This is my webpage.

<?php
  echo "This is written in PHP.\n";
?>
and making source file executable:
% chmod +x sample.cgi

The major difference between the two approaches is how the files are accessed by the webserver:
When using the .php extension, the script runs as the standard webserver user (commonly the user-id nobody or www-data). Therefore if the script attempts to access/create files, the programmer needs to make certain that the file permissions are set correctly.
When using the .cgi extension, the script runs as the owner of the script (you), so any files created/changed by the script will automatically be accessible by you.
Approach Two is the approach used in your department UNIX account on the students.csci.unt.edu server.
Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment