php modul-1

Download Php modul-1

If you can't read please download the document

Upload: kristophorus-hadiono

Post on 16-Apr-2017

1.091 views

Category:

Technology


0 download

TRANSCRIPT

PHP - TUTORIAL

Introduction

What is PHP ?PHP is an HTML-embedded scripting language.

PHP is a server side scripting language.

PHP syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in.

The goal of the language is to allow web developers to write dynamically generated pages quickly.

Introduction

What PHP do?When someone visits your PHP webpage, your web server processes the PHP code.

It then sees which parts it needs to show to visitors(content and pictures) and hides the other stuff (file operations, math calculations, etc.) then translates your PHP into HTML.

After the translation into HTML, it sends the webpage to your visitor's web browser.

Introduction

Introduction

What you need to learn PHP?SoftwareWeb server (Apache/Nginx/IIS)

PHP itself

PHP code editor (notepad++,context, etc.)

Database Server (MySQL/SQLite/SQLServer)

User skillKnow HTML syntax

Basic programming knowledge (not necesary)

Introduction

Characteristic of PHPSimplicity

Efficiency

Security

Flexibility

Familiarity

Introduction

Common use of PHPPerforms system functions, i.e. from files on a system it can create, open, read, write, and close them.

Can handle forms, i.e. gather data from files, save data to a file, thru email you can send data, return data to the user.

Add, delete, modify elements within your database thru PHP.

Access cookies variables and set cookies.

Using PHP, you can restrict users to access some pages of your website.

It can encrypt data.

Installing PHP

For Microsoft Windows userUse XAMPP/WAMPP for all in one web server(PHP, MySQL, Apache, phpMyAdmin)

Use Microsoft web server (IIS)

For Linux userRefers to your linux distribution repository or install one by one (PHP, MySQL, Apache, phpMyAdmin)

Using lamp package

How to save PHP files?

Always save the file with a .php extension instead of .html

Do not use word processor applications to create a php files.

Always use pure text editor such as notepad++, context, or specific IDE for PHP (komodo, crimson, aptana studio, etc.)

PHP - Syntax

What is Syntax ?The rules that must be followed to write properly structured code.

Standard syntax of PHP

Non-standard syntax of PHP (not recommended)

PHP - Syntax

SemicolonThe semicolon signifies the end of a PHP statement and should never be forgotten.

White spaceWhitespace is ignored between PHP statements.

This means it is OK to have one line of PHP code, then 20 lines of blank space before the next line of PHP code.

You can also press tab to indent your code and the PHP interpreter will ignore those spaces as well.

Sample of PHP code

Will display in browser:

Hello World!Hello World!

PHP Syntax

Use . (dot) to combine tokenToken is the smallest part of PHPExampleNumbers (124583)

Variables

Constants

Braces make blocksif (3 == 2 + 1){ echo "Good - I haven't totally"; echo "lost my mind.
";}

PHP Syntax

Commenting in PHPSingle comment //

Multiple/block comment /* Comment1Comment2 */

PHP Variables

What is Variables?A place in computer memory for storing a value (text/number)

Variables can, but do not need, to be declared before assignment.

Variables used before they are assigned have default values.

PHP does a good job of automatically converting types from one to another when necessary.

PHP variables are Perl-like.

Defining the variable$variable_name = Value;

Example$name=Kris;

$midTerm=77;

$final_grade=A;

PHP Variables

Naming conventionsmust start with a letter or underscore "_"

may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _

Variables with more than one word should be separated with underscores (ex. $my_variable)

Variables with more than one word can also be distinguished with capitalization (ex. $myVariable)

Php variables are case sensitive !

PHP data types

Integers: are whole numbers, without a decimal point, like 4195.

Doubles: are floating-point numbers, like 3.14159 or 49.1.

Booleans: have only two possible values either true or false.

NULL: is a special type that only has one value: NULL.

Strings: are sequences of characters, like 'PHP supports string operations.'

Arrays: are named and indexed collections of other values.

Objects: are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.

Resources: are special variables that hold references to resources external to PHP (such as database connections).

Output command

Echo commandTo send an output to screen, use 'echo' command

It can be used for variable or even a quotation string

ex.

Echo attention

Be careful when using HTML code or any other string that includes quotes

Use one of the following tactics if your string contains quotations:Don't use quotes inside your string

Escape your quotes that are within the string with a backslash. To escape a quote just place a backslash directly before the quotation mark, i.e. \"

Use single quotes (apostrophes) for quotes inside your string.

Echo attention

Ex.