Write A PHP Program To Keep Track of The Number of Visitors Visiting The Web Page and To Display This Count of Visitors, With Proper Headings
Write A PHP Program To Keep Track of The Number of Visitors Visiting The Web Page and To Display This Count of Visitors, With Proper Headings
What is PHP?
PHP is an acronym for "PHP: Hypertext Preprocessor"
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP is free to download and use
<?php
// PHP code goes here
?>
A PHP file normally contains HTML tags, and some PHP scripting code.
In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are not case-sensitive.
PHP – Variables
Variables are "containers" for storing information.
Note: When you assign a text value to a variable, put quotes around the value.
Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.
PHP Variables
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume).
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Float (floating point numbers - also called double) - A float (floating point number) is a number with a decimal point or a number in
exponential form.
Object - An object is a data type which stores data and information on how to process that data. In PHP, an object must be explicitly
declared.
NULL - Null is a special data type which can have only one value: NULL. A variable of data type NULL is a variable that has no
value assigned to it.
Resource - The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to
PHP. A common example of using the resource data type is a database call.
Multidimensional arrays
A multidimensional array is an array containing one or more arrays.
PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to
manage for most people.
Print/echo
With PHP, there are two basic ways to get output: echo and print.
echo and print are more or less the same. They are both used to output data to the screen.
The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple
parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.
Variables =(operator)
fopen()
The fopen() function opens a file or URL.
Syntax
fopen(filename, mode, include_path, context)
Parameter Values
Parameter Description
mode Required. Specifies the type of access you require to the file/stream.
Possible values:
"w" - Write only. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer at
the beginning of the file
"w+" - Read/Write. Opens and truncates the file; or creates a new file if it doesn't exist. Place file pointer
at the beginning of the file
"a" - Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist
"a+" - Read/Write. Preserves file content by writing to the end of the file
"x" - Write only. Creates a new file. Returns FALSE and an error if file already exists
"x+" - Read/Write. Creates a new file. Returns FALSE and an error if file already exists
"c" - Write only. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the beginning
of the file
"c+" - Read/Write. Opens the file; or creates a new file if it doesn't exist. Place file pointer at the
beginning of the file
include_path Optional. Set this parameter to '1' if you want to search for the file in the include_path (in php.ini) as well
context Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream
fscanf()
The fscanf() function parses the input from an open file according to the specified format.
Syntax
fscanf(file, format, mixed)
Parameter Values
Parameter Description
%o - Octal number
%s - String
Additional format values. These are placed between the % and the letter (example %.2f):
+ (Forces both + and - in front of numbers. By default, only negative numbers are marked)
' (Specifies what to use as padding. Default is space. Must be used together with the width specifier.
Example: %'x20s (this uses "x" as padding)
Note: If multiple additional format values are used, they must be in the same order as above.
mixed Optional.
fclose()
The fclose() function closes an open file.
Syntax
fclose(file)
Parameter Values
Parameter Description
php arrays
fprintf()
The fprintf() function writes a formatted string to a specified output stream (example: file or database).
Syntax
fprintf(stream,format,arg1,arg2,arg++)
Parameter Values
Parameter Description
format Required. Specifies the string and how to format the variables in it.
%g - shorter of %e and %f
%G - shorter of %E and %f
%o - Octal number
%s - String
%x - Hexadecimal number (lowercase letters)
Additional format values. These are placed between the % and the letter (example %.2f):
+ (Forces both + and - in front of numbers. By default, only negative numbers are marked)
' (Specifies what to use as padding. Default is space. Must be used together with the width specifier.
Example: %'x20s (this uses "x" as padding)
Note: If multiple additional format values are used, they must be in the same order as above.
arg1 Required. The argument to be inserted at the first %-sign in the format string
arg2 Optional. The argument to be inserted at the second %-sign in the format string
arg++ Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string
dot operator
In PHP, the string operator dot (.) is used to concatenate strings.
<meta> tags always go inside the <head> element, and are typically used to specify character set, page description, keywords, author of the
document, and viewport settings.
Metadata is used by browsers (how to display content or reload page), search engines (keywords), and other web services.
Attributes
charset character_set Specifies the character encoding for the HTML document
content text Specifies the value associated with the http-equiv or name attribute
http-equiv content-security-policy Provides an HTTP header for the information/value of the content attribute
content-type
default-style
refresh
Css
<p>
The <p> tag defines a paragraph.
Echo
date()
The date() function formats a local date and time, and returns the formatted date string.
Syntax
date(format, timestamp)
Parameter Values
Parameter Description
format Required. Specifies the format of the outputted date string. The following characters can be used:
S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)
a - Lowercase am or pm
A - Uppercase AM or PM
I (capital i) - Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)
Z - Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)
r - The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)
U - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
timestamp Optional. Specifies an integer Unix timestamp. Default is the current local time (time())