PHP3: Cross-platform server-side embedded scripting language

Source: Internet
Author: User
Tags ereg explode expression file system connect odbc mysql split

Server-side scripting technology is a very useful thing, combining it with client script technology to create a very powerful page. It seems like a long time ago, which version (appears to be the WWW edition) discussed ASP to the point of full swing, and someone proposed to open an ASP version, that is a server-side scripting technology. The usual server-side scripting techniques are cgi,nsapi/isapi/fastcgi and so on, and they are both improved and expanded. As to what language to implement the CGI technology, there is no provision, you generally use Perl, because its character is more powerful. To the need for speed of the occasion, the use of C to achieve.

A CGI script written in PERL/C is a "non-embedded" server-side script because it is a separate program, rather than embedded in an HTML document and then interpreted by another program for substitution.

For example, the following Perl program:
print "content-type:text/html\n\n";
Print <<HeadofHTML;
<HTML>
<HEAD>
<TITLE>Hello!</TITLE>
</HEAD>
<BODY>
Headofhtml
print "<CENTER>aaa</CENTER>";
print "</BODY></HTML>";
Although there is a paragraph in between it seems to be an HTML document, but that is the HTML document embedded in the Perl program, not the reverse. Idc/htx,asp is an embedded server-side script, and the following example is a msql Lite script:
<HTML>
<HEAD>
<TITIE>
<!
echo "hello!";
>
</TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>
This script,<!... > is replaced with its output after the server-side interpreter. PHP is a cross-platform server-side scripting technology. It was originally a person in order to write his home page with Perl written "packaging" program, later because the use of more people, it developed, is now 3.0 version, with C implementation, in UNIX and Windows 95/nt can run up.

The PHP3.0 has several platform-compiled versions, as well as source versions. PHP3.0 internal with several common database support, including: Sybase,oracle,msql,mysql,postgresql, odbc,dbase, etc. If you choose to support some of these databases when compiling its source code, you must have their client libraries in advance.


The way to compile on Unix is to unpack first, then run "./configure [Options]" (see the Readme file for options), and then run "make" and "Make install". The most important options are: "--enable-msql=msql library path", such as database options and "--enable-apache= Apache source program path" and so on. If you choose--enable-apache, then the compiled will be a library file, make install will install the library into the Apache source program, and then compile Apache, you can get support for PHP Apache server. Otherwise, the generated will be an interpreter.

     please see the Readme file for details.   Under Windows, there is a setup program, the interface is not very good, and a few files will report not found, skip it. Install the PHP3 directory to set up a network executable. The readme said to manually modify the registry, but it seems to use  .   Documents are not very good, because 3.0 of the documents are not fully written, and 2.0 of the documents are somewhat outdated.
   after installation, the system should support HTTP reads to. php3 and. Phps files. (in Unix, you need to modify the service   srm.conf file, see the Readme).


   if the Apache server is connected to the installation, the speed should be the fastest. If executed as a CGI program, the speed   will also be. If the system supports FASTCGI, the interpreter can also run as a fastcgi program. The interpreter for   supporting ISAPI is not yet out of windows.
  
     Now let's take a look at the first php3 script, as usual hello,world. 
   <HTML>
   <HEAD>
   <title>first PHP3 script</title>
   </ Head>
   <BODY>
   <CENTER>
  
   echo "Hello, world!";
  ?>
   </CENTER>
   </BODY>
   </HTML>
  
   put it in any directory that can be accessed through HTTP, named Hello.php3, and then connect to the browser   The file, you can see its effect (don't I write it.)
   If you rename it to Hello.phps and then connect to Netscape or Explorer, you can see the source code with the color  , because the interpreter handles it with a syntax highlight when it encounters the. phps file. Add a lot of color   control characters to the inside and send them out.   does not need to manually specify where the interpreter is located, because if you can specify it under the Apache server, srm.conf, under Microsoft IIS or PWS, the registry will be specified.


From the above example it should be easy to see how the PhP3 file is written. The part that is enclosed with the?> is the PHP3 program segment, where the interpreter does not process the remainder, and then executes the program segments in its output instead (the Echo statement outputs an unformatted string.  printf statements are similar to C's functions with the same name. As you can see, the PHP3 program is text, but the client can only see the result of the interpretation (unless you provide a Phps file with the same name), so it is confidential to the client, which is also the feature of most server-side scripts.


PHP, like the general explanatory language, does not need to define variables in advance. If you try to use a variable that has not been assigned a value, the value returned is an empty string. Assigning a variable also allocates the memory of the variable. PHP's variable name must have a $ number in front of it, which is also a common practice in many scripting languages.

For example:
$a = 1;
$b = 1.0;
$c = "hello!";
are valid PHP statements.
The arrays in PHP are defined in this way:
$a [1]= "a";
$a [2]= "B";
It will automatically expand without having to set a pre-set limit.
In fact, an array subscript can also be a string, for example:
$a ["a"]=1;
$a ["B"]= "hello!";
At this point it is actually a hash table, just like the so-called array in Tcl.
PHP has a pointer-like usage, such as:
$a = "B";
$ $a = "C";
The latter sentence produces a $b variable and assigns it a value of "C".
The operators of PHP variables are similar to C, for example:
+,-, *,/,%,^,&,|,&&,| |,!, +=,-=,++,--etc.
PHP3.0 defines a string addition operator ".", as in Perl.
PHP's variable type can be automatically converted, if you need to cast, the same approach as C. It has an integer,
There are three simple types of double,string.
To traverse a hash table, you can do this:
$i ["a"] = 0;
$i ["c"] = 1;
Reset ($i);
for ($j =1; $j <=count ($i); $j + +) {
$k = key ($i);
echo "$k, $i [$k]";
echo "<BR>";
Next ($i);
}
The control flow statements in PHP are almost exactly the same as C, and there are if...else,while,for,switch,break,
Exit.

The functions in PHP3.0 are as follows:
function test ($a, $b) {
return $a *2+ $b;
}
It's about the same as C.
Functions can be invoked recursively.
The PHP3.0 variable scope rule is similar to C, which means that the variables in the main program are global variables, and that the variables in the function are local variables, even if there is a global variable with the same name. If you want to use a global variable in a function, you must declare it with the global statement on the function header,

For example:
Function ... {
Global $a, $b;
...
}
If you declare a variable with a static statement on the function header, the variables are static and have the same meaning as in the C language. The include statement is provided in PHP, similar to the #include of C. An include statement can contain any file, and its contents appear on the page that is last printed. If there is a <?...? in the file  >, the part will be interpreted by the PHP interpreter, otherwise the contents of the file are sent out intact. It contains a file name that can be either an absolute or a relative path or an HTTP or FTP URL, in which case the interpreter automatically takes the URL content. In this way, it can even trigger a CGI program on another machine. The ReadFile statement is similar to include, but it does not execute the PHP program in the file, but only sends the file intact. The <?...? in the included file > will be interpreted as a comment by the browser.
The string manipulation functions in PHP3.0 are more important, as follows:
(1) echo,print,printf,sprintf
The first two functions are output strings. If there are variable names in the string, they are replaced with their values. The latter two functions are similar to C's with the same name function.
(2) Strchr,strlen,strtok,strrchr,strrev,strstr,strtolower,
Strtoupper,substr,ucfirst
These are commonly used string manipulation functions, some of which have exactly the same meaning as the functions of the same name in C. Strrev is to flip a string.  The meaning of Strtolower and strtoupper should not be explained.  Ucfirst the first character of a string into uppercase. SUBSTR is a substring of the return string, using the following: substr (string, header, length). The head position is from 0. If it is a negative number, it is from the tail forward.
(3) Chr,ord
Similar to C's function of the same name.
(4) Explode,implode,join
These are the functions that are related to the array. Explode (string, delimiter) returns an array of characters that are produced by separating the string from the separator. Implode (array, delimiter) returns a string that inserts a delimiter between the elements of an array. Join has the same meaning as implode.
(5) Chop
Removes whitespace from the tail of the string.
(6) Htmlspecialchars
Replace the HTML special characters in the string with their names, for example, "<" becomes "<".
(7) NL2BR
Precede each carriage return in the string with "<BR>".
(8) Addslashes,stripslashes
Add and remove "\" for characters in the string that need to be added "\" to the database query.
(9) Parse_str
Parse the string "Name1=value1&name2=value2&amp ..." into some variables.
For example:
Parse_str ("a=1&b=2");
Generates $a and $b two variables with a value of 1,2.  If there are two names that are part of the same name/value, then the last value overrides the previous one. If both pairs have "[]" at the end, such as "a[]=1&a[]=2", the array $a is generated, and the two elements are 1,2 respectively.

PHP is similar to other Cross-platform languages (perhaps Java is not in its column,:)) and has regular expression functionality. PHP3.0 's regular expression functionality is certainly far less than Perl's, but it's still sufficient, with the main functions:
(1) Ereg,eregi
This is a regular expression matching function, the former is case-insensitive, and the latter is irrelevant.
Usage:
Ereg (regular expression, string, [match part array name]); Regular expressions in PHP3.0 are generally similar to those used in grep.

(2) Ereg_replace,eregi_replace
These are replacement functions.
Usage:
Ereg_replace (Regular expression, substitution string, original string); A strtr in the string handler is a "translation" function, similar to the tr/.../.../in Perl,
Usage:
STRTR (String, "from", "to");
For example:
STRTR ("Aaabb", "AB", "CD") returns to "CCCDD".
(3) Split
It's similar to the explode function, but this time you can split the string at a place that matches a regular expression.
Usage:
Split (regular expression, string, [number of items taken out before]);

Title: php3: Cross-platform server-side embedded scripting language (8)

The file operation function in PHP3.0 is similar to C, but there are some extensions, especially in addition to supporting the access to the native files, but also to HTTP and FTP URLs to access, as long as the URL as a filename passed to the file operation function is OK.
The main file operation functions are:
(1) Fclose,feof,fgetc,fgets,fopen,fputs,fseek,ftell,mkdir,readlink,
Rename,rewind,rmdir,stat,unlink
These are similar to the functions of the same name in the C language.

(2) Chgrp,chmod,chown,copy
The meaning of these should also be easy to understand:
CHGRP (filename, group);
chmod (filename, mode);
Chown (filename, user);
Copy (source filename, target filename);
Note that these functions use a filename instead of the file number returned by fopen.
(3) File_exists,fileatime,filectime,filegroup,fileinode,filemtime,
Fileowner,filesize,filetype,fileperms,fileumask,is_dir,
Is_executable,is_file,is_link,is_readable,is_writeable
These are file information functions, most of which accept a filename as a parameter.
(4) FGETSS
Usage:
FGETSS (file number, maximum length);
Reads a file's line or until the maximum length (similar to fgets), but removes all
HTML and PHP tags.
(5) file
Usage:
file (filename);
Returns an array in which each element is a row in the file.
(6) Tempnam
Usage:
Tempnam (directory name, prefix);
Returns a temporary file name.
(7) Basename,dirname
Gets the file name section and the Directory name section in the path to the files.
Under Windows systems, both "/" and "\" can be used as directory delimiters, and only "/" under other systems.

The directory traversal function in PHP is also very common, but it has an "object-oriented" form, so also mention:
(1) Dir,opendir
Usage:
$d = Dir ("directory Name");
$handle = Opendir ("directory Name");
The former returns a directory object, which returns a directory handle.
The former returns an object that has handle and path two attributes, and the first is equivalent to Opendir
The handle returned, and the second is the directory name itself. Visit with $d->handle and $d->path.
(2) Read,readdir;rewind,rewinddir;close,closedir;
The first of each group in the three groups is the method of the directory object, called by the object-> method ().
The latter is a function, called by the function name (directory handle).
Read is the next filename in the directory.
Rewind is the first file name to return to the directory.
Close is the directory that is closed and is no longer traversed.
(3) ChDir
Convert PHP's working directory.

There are some of the time functions in PHP:
(1) Date
Usage:
Date (format, [time]);
If there is no time argument, the current time is used.
A format is a string in which the following characters have special meaning:
U replaces the number of seconds since a start time (as if it were January 1, 1970)
Y replaces the 4-digit reign.
Y replaces the 2-digit reign.
F replaces the full name of the month in English.
M replaces the English abbreviation of the month.
M replaces the number of months.
Z replaces the number of days since January 1 of the year.
D replaces the number of own.
L REPLACE the English full name of the week.
D replaces the English abbreviation of the day of the week.
W replaces the Day of the Week (number).
H replaces the number of hours (24-hour system).
H replaces the number of hours (12-hour system).
I replace the number of minutes.
s replaces the number of seconds.
A Replace with "AM" or "PM".
A replace with "AM" or "PM".
S replaces the ordinal number suffix, for example: "St", "nd", "rd", "th".
function returns a format string that has been replaced.
(2) getdate (time)
Returns a hash table, each subscript is:
"Seconds"-number of seconds
"Minutes"--fractions
"Hours"--Number of hours
"Mday"--Number of days
"Mon"--Number of months
"Year"--the reign of the year
"Yday"--number of days since January 1
"Weekday"--day of the week, English full name
"Month"--month, full name in English
(3) Gmdate
Similar to date, but first converts time to Greenwich standard.
(4) Mktime
Usage:
Mktime (hours, fractions, seconds, months, days, years);
Returns a time value that can be used for other functions.
(5) Time
Usage:
Time ();
Returns the number of seconds since Horizon January 1, 1970.
(6) Microtime
Usage:
Microtime ();
Returns a string that is divided into two parts with a space, and the latter part equals time ()
Return value, the first part is the number of microseconds.
(7) Checkdate
Usage:
Checkdate (month, day, year);
Returns logical TRUE or logical false.
If:
[1] The year between 1900 and 32767 (including 1900 with 32767);
[2] The month between 1 to 12;
[3] The day is within the allowable number of days of the month (considering a leap year);
Returns the logic true.
(8) Set_time_limit
Usage:
Set_time_limit (number of seconds);
Specifies that the program must run at the end of a specified number of seconds from the time the sentence is run, and that the program exit with a time-out.

PHP has a set of image functions, can dynamically generate GIF format image data stream and output to the server. For this set of functions to work, the system must have the GD library support. If it is under UNIX, you should obtain the GD source code and compile it before compiling PHP, and generate LIBGD.A and some. h files, respectively, to the system's library directory and header file directory (for example,/usr/lib and/usr/  If it is under Windows, the PHP3.0 installer installs a Gd.dll file and adds a ' DL ' (GD) to the program. DLL "); ' You can use an image function.
The main image functions are:
(1) Imagecreate (width, height)
Returns an image descriptor.
(2) Imagecreatefromgif (filename);
Returns an image descriptor.
(3) Imagecolorallocate (image descriptor, red, green, blue);
Returns a color descriptor. Because a GIF image can only have 256 colors, you must first assign a palette to it.
This statement is the allocation of a palette item.
(4) Imagecolortransparent (image descriptor, color descriptor);
Specifies that a color is a transparent color.
(5) Imagearc (Image descriptor, center horizontal axis, center ordinate, oval width, oval height, beginning
Angle, end angle, color descriptor);
Imagechar (image descriptor, font, x,y, character, color descriptor);
Imagecharup (image descriptor, font, x,y, character, color descriptor);
Imagecopyresized (target image descriptor, source image descriptor, target x, target y, source x, source Y,
The target is wide, the target is high, the source is wide, the source is high);
Imagedashedline (Image descriptor, x1,y1,x2,y2, color descriptor);
Imagefill (Image descriptor, starting point x, starting point y, color descriptor);
Imagefilledpolygon (Image descriptor, array of vertices, vertex number, color descriptor);
Imagefilledrectangle (Image descriptor, x1,y1,x2,y2, color descriptor);
Imagefilltoborder (Image descriptor, starting point x, starting point y, boundary color, fill color);
Imageline (Image descriptor, x1,y1,x2,y2, color descriptor);
Imagepolygon (Image descriptor, array of vertices, vertex number, color descriptor);
Imagerectangle (Image descriptor, x1,y1,x2,y2, color descriptor);
Imagesetpixel (Image descriptor, x,y, color descriptor);
Imagestring (image descriptor, font, x,y, string, color descriptor);
Imagestringup (image descriptor, font, x,y, string, color descriptor);

These are the drawing functions, which need to be interpreted in order to store the vertices of the polygons in sequence.
1th x, 1th y, 2nd x, 2nd y,...
(6) Imageloadfont (filename);
The file should be a bitmap font file that returns a font number. The system defaults to 1-5 font numbers and can be used directly.
(7) Imagesx,imagesy
The width and height of an image are obtained, and an image descriptor parameter is received.

(8) Imagecolorat (Image descriptor, x,y);
Imagecolorclosest (image descriptor, red, green, blue);
Imagecolorexact (image descriptor, red, green, blue);
Imagecolorset (image descriptor, color descriptor, red, green, blue);
Imagecolorsforindex (image descriptor, color descriptor);
Imagecolorstotal (image descriptor);
The top three returns a color descriptor. Imagecolorexact returns-1 if no match is found.
Imagecolorsforindex returns an array of three items, the elements being red, green, and blue.
Imagecolorstotal returns the total number of colors.
(9) Imagefontheight,imagefontwidth
Receives a font number as an argument.
(10) Imagegif (image descriptor, [filename]);
If no filename is available, the GIF stream is sent to the browser. At the beginning of the program there should be a sentence:
Header ("Content-type:image/gif")
(11) Imagedestroy (image descriptor);

There is a small bug in the image function (at least in PHP3.0RC and php3.0rc3 for UNIX source code, it has been found that the download file on www.php.net should have been changed), that is, Imagesetpixel always draw points at (Y,y).  Whatever the value of x is, this is not a big problem. PHP's database capabilities should be said to be one of its most useful features. It is characterized by the built-in support for many databases, rather than the need to expand again. Perl and Tcl are also common cross-platform languages, have strong character ability, the former character ability is stronger, the performance is higher, and the latter has GUI ability. They can all be expanded to support the database, but if you still want your application to have cross-platform capabilities, you need to expand on a variety of platforms, For example, on the UNIX platform for the expansion of Perl DBI package, under Windows 95/nt Perl expansion WIN32::ODBC Package, sometimes this is very troublesome. and PHP, with its built-in database support, saves programmers the hassle.
PHP3.0 supports the following databases:
Adabas_d,dbase,dbm,filepro,msql,mysql,sybase,oracle,postgresql,solid Additionally, the ODBC interface under Windows is supported. The documentation says that if you need to use Microsoft SQL Server, you can access it using either the Sybase interface or the ODBC interface. When compiling on UNIX, you should specify which database support to take when configure, and the system should have client programs (at least header files and library files) for those databases. The various databases, the access functions are not exactly the same, for example, those sql-based databases are clearly not accessible in the same way as dBASE or dbm. The following is a more detailed description of the access functions for several databases:
I. DBase
(1) dbase_create (filename, field array);
Each item in a field array is an array of the name, type, length, and precision of the field (the latter two are not necessarily necessary).
A field type is a character that has several values:
"L"--Logic, "M"--remark, "D"--date, "N"--Number, "C"--string returns a database descriptor, and returns False if it fails.
(2) Dbase_open (filename, open mode);
Open in the same way as in the open function of C.
Returns a database descriptor that returns False if it fails.
(3) Dbase_numfields (Database descriptor), Dbase_numrecords (database descriptor);
(4) Dbase_add_record (database descriptor, record);
The record is an array. Returns false for failure.
(5) Dbase_get_record (database descriptor, record number);
Returns an array with the subscript starting at 0. If the array is accessed with the subscript "deleted", you can get information about whether the record has been deleted.
(6) Dbase_delete_record (database descriptor, record number);
(7) Dbase_pack (database descriptor);
(8) Dbase_close (database descriptor);

Two, dbm
DBM is a non relational database under UNIX (at least I feel like this), it is actually a hash table in file form, each pair of name/value items is called a record.
DBM has several versions, such as NDBM,GDBM, and so on.
(1) Dbmopen (filename, logo);
The flag is "R" (read-only), "W" (writable) or "n" (new). Returns a database identifier.
(2) Dbmfirstkey (database identifier);
Gets the first name (the key) and returns a string.
(3) Dbmnextkey (database identifier, current key);
Returns the next key.
(4) dbmexists (database identifier, key);
Confirm that the key exists and return to True/false.
(5) Dbmfetch (database identifier, key);
Returns the value (string) of the key.
(6) Dbminsert (database identifier, key, value);
Inserts a record that returns false if the specified key already exists, otherwise returns true.
(7) Dbmreplace (database identifier, key, value);
Replaces a record that is inserted if it was not previously recorded.
(8) Dbmdelete (database identifier, key);
Deletes a record.
(9) Dbmclose (database identifier);

Three, mSQL and MySQL.
mSQL (mini SQL) is a small relational database that does not perform well and is not fully supported in SQL language, but is sufficient in some network database applications.  MySQL is a variant of msql, with improved performance and increased user access control. Both databases are accessed through the SQL language, but MySQL basically supports all SQL92 specifications, just "Select ... where select ..." Not supported, and some data types are expanded, while mSQL does not support any nested SQL statements, nor does IT support statements such as "Update set Column1=column1-1".
mSQL because it is simpler to run simple SQL statements faster than MySQL, and MySQL thread and index up and down, running complex SQL statements faster than the msql,postgresql,oracle and so on. Security, msql through the ACL file on each host users access rights, the default is all readable/write, and MySQL through a licensing library to set user name, password and access rights, more reliable.
mSQL storage capacity is not very clear, MySQL storage capacity is limited by the file system, for example, under Linux can not be more than 2G (3G? not remember).
Their advantage is that they are available for free and installed on Linux, but pay for commercial use. PostgreSQL is also a free database under Linux, REDHAT5 inside the belt, but I have not used, will not say.
mSQL and MySQL since it is almost two things, PHP access to their statements are similar, for example, Msql_close and Mysql_close respectively to complete the same close action. So the following introduction is only for MySQL introduction, mSQL Access statements can only be replaced with a prefix (otherwise specified). Note: Both mSQL and MySQL access functions need to have the appropriate permissions to run.
(1) mysql_connect (host, username, password);
Returns a connection number.
Note: The password for each user of MySQL can vary depending on the IP address of the user's machine. In addition, mSQL does not have a user name mechanism, so msql_connect only needs one host parameter. The host can be an IP address or a domain name.
(2) mysql_create_db (database name);
(3) mysql_select_db (database name, connection number);
Connect to a database.
(4) mysql_query (SQL statement, connection number);
If the SQL statement is a SELECT, a result number is returned. Otherwise the returned value can be ignored. Returns False if it fails.
(5) Mysql_fetch_array (result number);
Remove the next row and return an array. Can be accessed with a digital subscript (the first field is subscript 0), or you can use a string subscript access (that is, use each field name).
Returns False if the last row has been taken.
(6) Mysql_fetch_field (Result number, [field ordinal]);
If no field number, remove a field. Returns a hash table with the following label:
Name,table,max_length,not_null,primary_key,unique_key,
Multiple_key,numeric,blob,type,unsigned,zerofill
The meaning of each subscript should be more clear.
(7) Mysql_num_rows (result number); Mysql_num_fields (result number);
(8) Mysql_free_result (result number);
(9) Mysql_list_dbs (); mysql_list_tables (database name);
(a) Mysql_close (connection number);
(one) Mysql_pconnect (host, username, password);
Is exactly like Mysql_connect, but establishes a "permanent connection" that is never closed once established, even when the Mysql_close function or program is executed. The next time you attempt to establish a permanent connection, the system discovers that a permanent connection already exists. The connection number is returned directly without being recreated.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.