0% found this document useful (0 votes)
489 views

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

This document provides an overview of PHP and how to use it for server-side programming. It discusses PHP syntax and structure, variables, data types, arrays, printing output, opening and reading files. The key points are that PHP is a widely-used open source scripting language executed on the server, it has variables, operators, branching, arrays and object-oriented capabilities, and can be used to open, read, and write files.

Uploaded by

Yashu Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
489 views

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

This document provides an overview of PHP and how to use it for server-side programming. It discusses PHP syntax and structure, variables, data types, arrays, printing output, opening and reading files. The key points are that PHP is a widely-used open source scripting language executed on the server, it has variables, operators, branching, arrays and object-oriented capabilities, and can be used to open, read, and write files.

Uploaded by

Yashu Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

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.


 Server side programming adds a slightly different dimension to ‘standard’ programming encountered at
level 1, which generally consists of desktop applications. The server request/response model is introduced
to demonstrate how all script is run at once and output is generated.
 Students new to server side scripting will be given an overview of how PHP works, and where it fits into
the client/server request/response model. This should also serve as a good refresher for those who have
done some scripting prior to this module.
 Finally, the lecture delivers the language basics of PHP, the common constructs typical of most powerful
programming languages, that are necessary building blocks for any application. Such topics include the
syntax, variables, constants, operators and decision making (branching). With this basic knowledge
introduced, how this relates to the XHTML they have done so far.
 Ordinarily the client/response model merely serves a text file over the HTTP protocol to the user agent
application, which then renders the output appropriately. The protocol is inherently stateless (meaning
that each request/response is dealt with individually).

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 is an amazing and popular language!


 It is powerful enough to be at the core of the biggest blogging system on the web
(WordPress)!
 It is deep enough to run the largest social network (Facebook)!
 It is also easy enough to be a beginner's first server side language!

PHP - Syntax and Structure


A PHP script is executed on the server, and the plain HTML result is sent back to the browser.

Basic PHP Syntax

A PHP script can be placed anywhere in the document.

A PHP script starts with <?php and ends with ?>:

<?php
// PHP code goes here
?>

The default file extension for PHP files is ".php".

A PHP file normally contains HTML tags, and some PHP scripting code.

PHP Case Sensitivity

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.

Creating (Declaring) PHP Variables


In PHP, a variable starts with the $ sign, followed by the name of the variable

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).

Rules for PHP variables:

 A variable starts with the $ sign, followed by the name of the variable


 A variable name must start with a letter or the underscore character

 A variable name cannot start with a number

 A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

 Variable names are case-sensitive ($age and $AGE are two different variables)

PHP - Data Types


Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

 String - A string is a sequence of characters, like "Hello world!".


 Integer - An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647

 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.

 Boolean - A Boolean represents two possible states: TRUE or FALSE.

 Array - An array stores multiple values in one single variable.

 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.

Create an Array in PHP


In PHP, the array() function is used to create an array:
array();
In PHP, there are three types of arrays:
 Indexed arrays - Arrays with a numeric index
 Associative arrays - Arrays with named keys
 Multidimensional arrays - Arrays containing one or more arrays

PHP Indexed Arrays


There are two ways to create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
$cars = array("Volvo", "BMW", "Toyota");
or the index can be assigned manually:
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";

PHP Associative Arrays


Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array: 
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or:
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

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.

The PHP echo Statement


The echo statement can be used with or without parentheses: echo or echo().

The PHP print Statement


The print statement can be used with or without parentheses: print or print().

Variables =(operator)

fopen()
The fopen() function opens a file or URL.

Syntax
fopen(filename, mode, include_path, context)
Parameter Values

Parameter Description

filename Required. Specifies the file or URL to open

mode Required. Specifies the type of access you require to the file/stream.

Possible values:

 "r" - Read only. Starts at the beginning of the file


 "r+" - Read/Write. Starts at the beginning of the file

 "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

 "e" - Only available in PHP compiled on POSIX.1-2008 conform systems.

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

file Required. Specifies the file to check


format Required. Specifies the format.

Possible format values:

 %% - Returns a percent sign


 %b - Binary number

 %c - The character according to the ASCII value

 %d - Signed decimal number

 %e - Scientific notation (e.g. 1.2e+2)

 %u - Unsigned decimal number

 %f - Floating-point number (local settings aware)

 %F - Floating-point number (not local settings aware)

 %o - Octal number

 %s - String

 %x - Hexadecimal number (lowercase letters)

 %X - Hexadecimal number (uppercase 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)

 - (Left-justifies the variable value)

 [0-9] (Specifies the minimum width held of to the variable value)

 .[0-9] (Specifies the number of decimal digits or maximum string length)

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

file Required. Specifies the file to close

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

stream Required. Specifies where to write/output the string

format Required. Specifies the string and how to format the variables in it.

Possible format values:

 %% - Returns a percent sign


 %b - Binary number

 %c - The character according to the ASCII value

 %d - Signed decimal number (negative, zero or positive)

 %e - Scientific notation using a lowercase (e.g. 1.2e+2)

 %E - Scientific notation using a uppercase (e.g. 1.2E+2)

 %u - Unsigned decimal number (equal to or greather than zero)

 %f - Floating-point number (local settings aware)

 %F - Floating-point number (not local settings aware)

 %g - shorter of %e and %f

 %G - shorter of %E and %f

 %o - Octal number

 %s - String
 %x - Hexadecimal number (lowercase letters)

 %X - Hexadecimal number (uppercase 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)

 - (Left-justifies the variable value)

 [0-9] (Specifies the minimum width held of to the variable value)

 .[0-9] (Specifies the number of decimal digits or maximum string length)

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 http-equiv="refresh" content= “ 1“ />


The <meta> tag defines metadata about an HTML document. Metadata is data (information) about data.

<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 will not be displayed on the page, but is machine parsable.

Metadata is used by browsers (how to display content or reload page), search engines (keywords), and other web services.

Attributes

Attribute Value Description

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

name application-name Specifies a name for the metadata


author
description
generator
keywords
viewport

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:

 d - The day of the month (from 01 to 31)


 D - A textual representation of a day (three letters)

 j - The day of the month without leading zeros (1 to 31)

 l (lowercase 'L') - A full textual representation of a day

 N - The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)

 S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)

 w - A numeric representation of the day (0 for Sunday, 6 for Saturday)

 z - The day of the year (from 0 through 365)

 W - The ISO-8601 week number of year (weeks starting on Monday)


 F - A full textual representation of a month (January through December)

 m - A numeric representation of a month (from 01 to 12)

 M - A short textual representation of a month (three letters)

 n - A numeric representation of a month, without leading zeros (1 to 12)

 t - The number of days in the given month

 L - Whether it's a leap year (1 if it is a leap year, 0 otherwise)

 o - The ISO-8601 year number

 Y - A four digit representation of a year

 y - A two digit representation of a year

 a - Lowercase am or pm

 A - Uppercase AM or PM

 B - Swatch Internet time (000 to 999)

 g - 12-hour format of an hour (1 to 12)

 G - 24-hour format of an hour (0 to 23)

 h - 12-hour format of an hour (01 to 12)

 H - 24-hour format of an hour (00 to 23)

 i - Minutes with leading zeros (00 to 59)

 s - Seconds, with leading zeros (00 to 59)

 u - Microseconds (added in PHP 5.2.2)

 e - The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)

 I (capital i) - Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)

 O - Difference to Greenwich time (GMT) in hours (Example: +0100)

 P - Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)

 T - Timezone abbreviations (Examples: EST, MDT)

 Z - Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)

 c - The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)

 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())

You might also like