PHP & MySQL OLD
PHP or "PHP: Hypertext Preprocessor" is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Its syntax draws upon C, Java, and Perl, and is easy to learn. The main goal of the language is to allow web developers to write dynamically generated web pages quickly, but you can do much more with PHP. MySQL is a RDBMS(Relational database management system). We use such a system in order to store, retrieve and manage data related to our applications. Apache is an Open Source web server in wide usage. A web-server is required to host a web-application(webpage) on a remote server and is responsible for processing the request from the client, who wants to access the application(or webpage).
Contents
- 1 PHP
- 2 2. PHP chr() Function
- 3 4. PHP print() Function
- 4 5. PHP str_ireplace() Function
- 5 6. PHP str_pad() Function
- 6 7. PHP str_repeat() Function
- 7 8. PHP str_replace() Function
- 8 9. PHP str_shuffle() Function
- 9 10. PHP str_word_count() Function
- 10 11. PHP strcasecmp() Function
- 11 12. PHP strtolower() Function
- 12 13. PHP strtoupper() Function
- 13 14. PHP substr() Function
- 14 15. PHP ucwords() Function
- 15 16. PHP trim() Function
- 16 1. PHP Date() Function
- 17 2. PHP checkdate() Function
- 18 3. PHP date_default_timezone_get() Function
- 19 4. PHP date_default_timezone_set() Function
- 20 5. PHP getdate() Function
- 21 6. PHP date() Function
- 22 7. PHP localtime() Function
- 23 8. PHP time() Function
- 23.1 Definition and Usage
- 23.2 Syntax
- 23.3 Tips and Notes
- 23.4 Example
- 23.4.1 /Operators and Expressions/
- 23.4.2 /Conditional statements: If-Else, Switch-case/
- 23.4.3 /Loops: For, While, Do-while/
- 23.4.4 /Foreach, Break-continue /
- 23.4.5 /Break and continue statements/
- 23.4.6 /Arrays/
- 23.4.7 /Functions/
- 23.4.8 /Form Handling/
- 23.4.9 /A PHP form example 1/
- 23.4.10 /A PHP form example 2/
- 23.5 Intermediate Level
- 24 1. Date() Function
- 25 2. checkdate() Function
- 26 3. date_default_timezone_get() Function
- 27 4. date_default_timezone_set() Function
- 28 5. getdate() Function
- 29 6. date() Function
- 30 7. localtime() Function
- 31 8. time() Function
- 31.1 Definition and Usage
- 31.2 Syntax
- 31.3 Tips and Notes
- 31.4 Example
- 31.4.1 /File Handling/
- 31.4.2 /Exception Handling/
- 31.4.3 /PHP Function Library/
- 31.4.4 /Object Oriented Programming(OOPS) in PHP/
- 31.4.5 /Classes, Constructors and Methods,/
- 31.4.6 /Creating Instances./
- 31.4.7 /Variable scope within classes/
- 31.4.8 /Inheritance: the extends operator/
- 31.4.9 /Static methods and variables /
- 31.5 Advanced Level
- 32 MySQL
- 33 PHP & MySQL Integration
PHP
Basic Level
These tutorials would help you get started with PHP programming. In this section we would go through the basics of installing and getting PHP ready for development, and the basic syntax and features of the language.
/Introduction to PHP, XAMPP and MySQL/
- What is each of the above
- Why each of the above
/Setting up PHP,MySQL and Apache in Windows/
- Minimum requirements of the system.
- Installation and configuring PHP, Apache and MySQL (native installation)in Windows.
- Using XAMPP and MySQL GUI Tools on Windows.
/Setting up PHP,MySQL and Apache in Linux/
- Minimum requirements of the system.
- Installation and configuring PHP, Apache and MySQL(native installation)in Linux.
- Using XAMPP and MySQL GUI Tools on Linux.
/Hello World/
Procedure to embed into a HTML source. Introduction to echo command.
/Datatypes, variables and constants/
Introduction to the various datatypes Syntax for declaration and usage of variables and constants.
/String Operations /
2. PHP chr() Function
Definition and Usage
The chr() function returns a character from the specified ASCII value.
Syntax
chr(ascii) |
Parameter | Description |
ascii | Required. An ASCII value |
Tips and Notes
Note: The x parameter can be specified in decimal, octal, or hex values. Octal values are defined by a leading 0, while hex values are defined by a leading 0x.
Example
<?phpecho chr(52)."<br />";echo chr(052)."<br />";echo chr(0x52)."<br />";?> |
The output of the code above will be:
4. PHP print() Function
Definition and Usage
The print() function outputs one or more strings.
Syntax
print(strings) |
Parameter | Description |
strings | Required. One or more strings to be sent to the output |
Tips and Notes
Note: The print() function is not actually a function, so you are not required to use parentheses with it.
Tip: The print() function is slightly slower than echo().
Example 1
<?php$str = "Who's Kai Jim?";print $str;print "<br />";print $str."<br />I don't know!";?> |
The output of the code above will be:
Who's Kai Jim?Who's Kai Jim?I don't know! |
Example 3
Difference of single and double quotes. Single quotes will print the variable name, not the value:
<?php$color = "red";print "Roses are $color";print "<br />";print 'Roses are $color';?> |
The output of the code above will be:
Roses are redRoses are $color
5. PHP str_ireplace() FunctionDefinition and UsageThe str_ireplace() function replaces some characters with some other characters in a string. Syntax
Tips and NotesNote: This function is case-insensitive. Use str_replace() to perform a case-sensitive search. Note: This function is binary-safe. Example 1
The output of the code above will be:
Example 2In this example we will demonstrate str_ireplace() with an array and a count variable:
The output of the code above will be:
|
6. PHP str_pad() Function
Definition and Usage
The str_pad() function pads a string to a new length.
Syntax
str_pad(string,length,pad_string,pad_type)
|
Parameter | Description |
string | Required. Specifies the string to pad |
length | Required. Specifies the new string length. If this value is less than the original length of the string, nothing will be done |
pad_string | Optional. Specifies the string to use for padding. Default is whitespace |
pad_type | Optional. Specifies what side to pad the string.
Possible values:
|
Example 1
<?php$str = "Hello World";echo str_pad($str,20,".");?> |
The output of the code above will be:
Hello World......... |
Example 2
<?php$str = "Hello World";echo str_pad($str,20,".",STR_PAD_LEFT);?> |
The output of the code above will be:
.........Hello World |
Example 3
<?php$str = "Hello World";echo str_pad($str,20,".:",STR_PAD_BOTH);?> |
The output of the code above will be:
.:.:Hello World.:.:.
7. PHP str_repeat() FunctionDefinition and UsageThe str_repeat() function repeats a string a specified number of times. Syntax
Example
The output of the code above will be:
|
8. PHP str_replace() Function
Definition and Usage
Syntax
str_replace(find,replace,string,count) |
Parameter | Description |
find | Required. Specifies the value to find |
replace | Required. Specifies the value to replace the value in find |
string | Required. Specifies the string to be searched |
count | Optional. A variable that counts the number of replacements |
Tips and Notes
Note: This function is case-sensitive. Use str_ireplace() to perform a case-insensitive search.
Note: This function is binary-safe.
Example 1
<?phpecho str_replace("world","aaditya","Hello world!");?> |
The output of the code above will be:
Hello aaditya! |
Example 2
In this example we will demonstrate str_replace() with an array and a count variable:
<?php$arr = array("blue","red","green","yellow");print_r(str_replace("red","pink",$arr,$i));echo "Replacements: $i";?> |
The output of the code above will be:
Array([0] => blue[1] => pink[2] => green[3] => yellow)Replacements: 1 |
9. PHP str_shuffle() Function
Definition and Usage
The str_shuffle() function randomly shuffles all the characters of a string.
Syntax : - str_shuffle(string) |
Parameter | Description |
string | Required. Specifies the string to shuffle |
Example
<?phpecho str_shuffle("Hello World");?> |
The output of the code above could be:
H leooWlrld |
10. PHP str_word_count() Function
Definition and Usage
The str_word_count() function counts the number of words in a string.
Syntax
str_word_count(string,return) |
Parameter | Description |
string | Required. Specifies the string to check |
return | Optional. Specifies the return value of the str_word_count() function.
Possible values:
|
Example 1
<?phpecho str_word_count("Hello world!");?> |
The output of the code above will be:
2 |
Example 2
<?phpprint_r(str_word_count("Hello world!",1));?> |
The output of the code above will be:
Array([0] => Hello[1] => world) |
Example 3
<?phpprint_r(str_word_count("Hello world!",2));?> |
The output of the code above will be:
Array([0] => Hello[6] => world) |
11. PHP strcasecmp() Function
Definition and Usage
The strcasecmp() function compares two strings.
This function returns:
- 0 - if the two strings are equal
- <0 - if string1 is less than string2
- >0 - if string1 is greater than string2
Syntax ; -strcasecmp(string1,string2) |
Parameter | Description |
string1 | Required. Specifies the first string to compare |
string2 | Required. Specifies the second string to compare |
Tips and Notes
Tip: The strcasecmp() function is binary safe and case-insensitive. USE strcmp for case-sensitive
Example
<?phpecho strcasecmp("Hello world!","HELLO WORLD!");?> |
The output of the code above will be:
0
12. PHP strtolower() FunctionDefinition and UsageThe strtolower() function converts a string to lowercase. Syntax
Example
The output of the code above will be:
13. PHP strtoupper() FunctionDefinition and UsageThe strtoupper() function converts a string to uppercase. Syntax
Example
The output of the code above will be:
|
15. PHP ucwords() Function
Definition and Usage
The ucwords() function converts the first character of each word in a string to uppercase.
Syntax
ucwords(string) |
Parameter | Description |
string | Required. Specifies the string to convert |
Example
<?phpecho ucwords("hello world");?> |
The output of the code above will be:
Hello World
16. PHP trim() FunctionDefinition and UsageThe trim() function removes whitespaces and other predefined characters from both sides of a string. Syntax
Example 1
The browser output of the code above will be:
If you select "View source" in the browser window, you will see the following HTML:
|
1. PHP Date() Function
The PHP date() function is used to format a time and/or date.
The PHP Date() Function
The PHP date() function formats a timestamp to a more readable date and time.
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
Syntax
date(format,timestamp) |
Parameter | Description |
format | Required. Specifies the format of the timestamp |
timestamp | Optional. Specifies a timestamp. Default is the current date and time |
PHP Date() - Format the Date
The required format parameter in the date() function specifies how to format the date/time.
Here are some characters that can be used:
- d - Represents the day of the month (01 to 31)
- m - Represents a month (01 to 12)
- Y - Represents a year (in four digits)
<?phpecho date("Y/m/d") . "<br />";echo date("Y.m.d") . "<br />";echo date("Y-m-d")?> |
The output of the code above could be something like this:
2009/05/112009.05.112009-05-11 |
PHP Date() - Adding a Timestamp
The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.
The mktime() function returns the Unix timestamp for a date.
The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax for mktime()
mktime(hour,minute,second,month,day,year,is_dst) |
To go one day in the future we simply add one to the day argument of mktime():
<?php$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));echo "Tomorrow is ".date("Y/m/d", $tomorrow);?> |
The output of the code above could be something like this:
Tomorrow is 2009/05/12 |
2. PHP checkdate() Function
Definition and Usage
The checkdate() function returns true if the specified date is valid, and false otherwise.
A date is valid if:
- month is between 1 and 12 inclusive
- day is within the allowed number of days for the particular month
- year is between 1 and 32767 inclusive
Syntax
checkdate(month,day,year) |
Parameter | Description |
month | Required. Specifies the month |
day | Required. Specifies the day |
year | Required. Specifies the year |
Example
<?php
if(checkdate(12,31,2000)) echo "True"; else echo "False"; if (checkdate(2,29,2003)) echo "true"; else echo "False"; if( checkdate(2,29,2004)) echo "True"; else echo "False"; ?> |
The output of the code above will be:
True
False True 3. PHP date_default_timezone_get() FunctionDefinition and UsageThe date_default_timezone_get() function returns the default timezone used by all date/time functions in a script. Syntax
Example
The output of the code above will be:
|
/Operators and Expressions/
Exploring operator concept: 9 types Precedence of operators Exploring expressions
/Conditional statements: If-Else, Switch-case/
/Loops: For, While, Do-while/
/Foreach, Break-continue /
/Break and continue statements/
/Arrays/
/Functions/
Declaring a function Passing a value Returning value
/Form Handling/
/A PHP form example 1/
/A PHP form example 2/
Intermediate Level
/Including Files/
/Sessions/
/Cookies/
/Email/
/Date and Time/
1. Date() Function
The PHP date() function is used to format a time and/or date.
The PHP Date() Function
The PHP date() function formats a timestamp to a more readable date and time.
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
Syntax
date(format,timestamp) |
Parameter | Description |
format | Required. Specifies the format of the timestamp |
timestamp | Optional. Specifies a timestamp. Default is the current date and time |
PHP Date() - Format the Date
The required format parameter in the date() function specifies how to format the date/time.
Here are some characters that can be used:
- d - Represents the day of the month (01 to 31)
- m - Represents a month (01 to 12)
- Y - Represents a year (in four digits)
<?phpecho date("Y/m/d") . "<br />";echo date("Y.m.d") . "<br />";echo date("Y-m-d")?> |
The output of the code above could be something like this:
2009/05/112009.05.112009-05-11 |
PHP Date() - Adding a Timestamp
The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.
The mktime() function returns the Unix timestamp for a date.
The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax for mktime()
mktime(hour,minute,second,month,day,year,is_dst) |
To go one day in the future we simply add one to the day argument of mktime():
<?php$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));echo "Tomorrow is ".date("Y/m/d", $tomorrow);?> |
The output of the code above could be something like this:
Tomorrow is 2009/05/12 |
2. checkdate() Function
Definition and Usage
The checkdate() function returns true if the specified date is valid, and false otherwise.
A date is valid if:
- month is between 1 and 12 inclusive
- day is within the allowed number of days for the particular month
- year is between 1 and 32767 inclusive
Syntax
checkdate(month,day,year) |
Parameter | Description |
month | Required. Specifies the month |
day | Required. Specifies the day |
year | Required. Specifies the year |
Example
<?php
if(checkdate(12,31,2000)) echo "True"; else echo "False"; if (checkdate(2,29,2003)) echo "true"; else echo "False"; if( checkdate(2,29,2004)) echo "True"; else echo "False"; ?> |
The output of the code above will be:
True
False True 3. date_default_timezone_get() FunctionDefinition and UsageThe date_default_timezone_get() function returns the default timezone used by all date/time functions in a script. Syntax
Example
The output of the code above will be:
|
/File Handling/
/Exception Handling/
/PHP Function Library/
/Object Oriented Programming(OOPS) in PHP/
/Classes, Constructors and Methods,/
/Creating Instances./
/Variable scope within classes/
/Inheritance: the extends operator/
/Static methods and variables /
Advanced Level
/CakePHP: A PHP Framework/
Zend programming conventions Hands on sessions Examples
MySQL
Database Concepts
- Entity - Relationship Model (ERM)
- Primary And Foreign Keys
- 1 st Normal Form [1 NF]
- 2 nd Normal Form [2 NF]
- 3 rd Normal Form [3 NF]
- Overview of Denormalization
MySQL as DB Platform
MySQL Data Types
Administrative MySQL Command
MySQL Database Commands
/Basics of SQL/
Brief introduction to SQL and RDBMS
- About SQL and RDBMS
- Relational Databases
- Primary keys
- Foreign keys
- Relationships
Simple Queries
- Create table
- Insert table into table values
- Delete
- Update table
- Drop table
/MySQL/
MYSQL: Working with GUI tools Administrative tools Protection of databases
Intermediate Level
Introduction to views and joints
- Create view
- Using multiple tables while executing query
Advanced Level
PHP & MySQL Integration
Connecting to MYSQL using PHP Executing queries Error handling