PHP

¡Supera tus tareas y exámenes ahora con Quizwiz!

PHP Variables Scope

In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used.

Concatenate in php

Instead of + use .

PHP 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. We will not talk about the resource type here, since it is an advanced topic.

What do I need?

To start using PHP, you can: Find a web host with PHP and MySQL support Install a web server on your own PC, and the install PHP and MYSQL

PHP 7 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: <?php $txt = "Hello world!"; $x = 5; $y = 10.5; ?>

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 Integer Float (floating point numbers - also called double) Boolean Array Object NULL Resource

Important note about PHP

With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.

Link for PHP w3school

https://www.w3schools.com/php7/php7_install.asp

Three different variable scopes:

local global static

PHP Boolean A Boolean represents two possible states: TRUE or FALSE.

$x = true; $y = false;

Case sensitivity on variables names

However, all variables names are case sensitive In the example below, only the first statement will display the value of the $color variable ($color, $COLOR, and $coLOR as treated as three different variables).

Setup PHP on your Own PC

However, if your server does not support PHP, you must: Install a web server Install PHP Install a database, such as MySQL The official PHP website (PHP.net) has installation instructions for PHP.

PHP Case Sensitivity

IN PHP, No keywords (if, else, while, echo, etc), classes, functions and user defined functions are case sensitive. In the example below, all three echo statements below are legal (and equal) <!DOCTYPE html> <html> <body> <?php ECHO "Hello World!<br>"; echo "Hello World!<br>"; EcHo "Hello World!<br>"; ?> </body> </html>

Output Variables

The PHP echo statement is often used to output data to the screen. The following example will show how to output text and a variable <?php $txt = "Hello world!"; $x = 5; $y = 10.5; ?> <!DOCTYPE html> <html> <body> <?php $x = 5; $y = 4; echo $x + $y; ?> </body> </html> Result for the second code will 9

THE PHP echo statement

The echo statement can be used with or without parentheses: echo or echo(). <!DOCTYPE html> <html> <body> <?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This ", "string ", "was ", "made ", "with multiple parameters."; ?> </body> </html> The following example shows how to output text and variables with the echo statement: <?php $txt1 = "Learn PHP"; $txt2 = "W3Schools.com"; $x = 5; $y = 4; echo "<h2>" . $txt1 . "</h2>"; echo "Study PHP at " . $txt2 . "<br>"; echo $x + $y; ?>

Print line on page functinality

<!DOCTYPE html> <html> <body> <?php echo "My first PHP script!"; ?> </body> </html>

PHP Float A float (floating point number) is a number with a decimal point or a number in exponential form. In the following example $x is a float. The PHP var_dump() function returns the data type and value:

<?php $x = 10.365; var_dump($x); ?>

PHP NULL Value 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. Tip: If a variable is created without a value, it is automatically assigned a value of NULL. Variables can also be emptied by setting the value to NULL:

<?php $x = "Hello world!"; $x = null; var_dump($x); ?>

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly. The example above can be rewritten like this:

<?php $x = 5; $y = 10; function myTest() { $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y']; } myTest(); echo $y; // outputs 15 ?>

PHP 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. First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods:

<?php class Car { function Car() { $this->model = "VW"; } } // create an object $herbie = new Car(); // show object properties echo $herbie->model; ?>

PHP Array An array stores multiple values in one single variable. In the following example $cars is an array. The PHP var_dump() function returns the data type and value:

<?php $cars = array("Volvo","BMW","Toyota"); var_dump($cars); ?>

PHP String A string is a sequence of characters, like "Hello world!". A string can be any text inside quotes. You can use single or double quotes:

<?php $x = "Hello world!"; $y = 'Hello world!'; echo $x; echo "<br>"; echo $y; ?>

PHP Integer An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. Rules for integers: An integer must have at least one digit An integer must not have a decimal point An integer can be either positive or negative Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based - prefixed with 0x) or octal (8-based - prefixed with 0) In the following example $x is an integer. The PHP var_dump() function returns the data type and value:

<?php $x = 5985; var_dump($x); ?>

Basic PHP Syntax

A PHP script is executed on the server, and the plain HTML result is sent back to the browser. A PHP script can be placed anywhere in the docuement. A PHP scrpt starts with <?php and ends with ?> <?php //PHP code goes here ?> The default file extension from PHP file is ".php" A PHP file normally contains HTML tags, and some PHP scripting code. Below, we have an example of a simpe PHP file, with a PHP script that uses a built in PHP function "echo" to output the text "Hello World! " on a web page: <!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> <?php echo "Hello World!"; ?> </body> </html>

Comments in PHP

A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone who is looking at the code. PHP supports several ways of commenting: <!DOCTYPE html> <html> <body> <?php // This is a single-line comment # This is also a single-line comment /* This is a multiple-lines comment block that spans over multiple lines */ // You can also use comments to leave out parts of a code line $x = 5 /* + 15 */ + 5; echo $x; ?> </body> </html>

Global Scope

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function: <!DOCTYPE html> <html> <body> <?php $x = 5; $y = 4; echo $x + $y; ?> </body> </html> The error means that the value just does not show up.

Local Scope

A variable declared within a function has a LOCA SCOPE and can only be accessed within that function: <?php function myTest() { $x = 5; // local scope echo "<p>Variable x inside function is: $x</p>"; } myTest(); // using x outside the function will generate an error echo "<p>Variable x outside function is: $x</p>"; ?>

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 _) Variables names are case-sensitive ($age and $AGE are two different values)

Use a web host with PHP support

If your server has activated suppport for PHP you do not need to do anything. Just create some .php files, place them in your web directory, and the server will automaticaly parse them for you. You do not need to compile anything or install any exxtra tools. Because PHP is free, msot web hosts offer PHP support.

What is added in PHP 7?

In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on type mismatch. You will learn more about the strict, and non-strict requirements, and the data type declartions in the PHP 7 Functions chapter.

PHP 7 echo and print Statements

In PHP there are two basic ways to get output: echo and print. In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.

PHP is a loosely typed language

In the example above, notice that we did not have to tell PHP which data type the variable is. PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like a string to an integer without causing an error.

PHP The Static Keyword

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static keyword when you first declare the variable: <?php function myTest() { static $x = 0; echo $x; $x++; } myTest(); myTest(); myTest(); ?> When hits static again it just uses previous stored or calculated value. Then, each time the function is called, that variable will still have the information it contained from the last time the function was called. Note: The variablle is still local the function.

What's new in PHP 7

PHP 7 is much faster that the previous popular stable release (PHP 5.6) PHP 7 has improved Error handling PHP 7 supports stricter Type Declarations for function arguements PHP 7 supports new operator (like the spaceship operator <=>) and much more!

What can PHP do?

PHP can generate dynamic page content PHP can create, open, read, write, delete and close files on the server PHP can collect form data PHP can send and receive cookies PHP can add, delete, modify data in you database PHP can be used to control user-access PHP can encrypt data

What is a PHP file?

PHP file can contain text, HTML, CSS, Javascript and PHP code PHP code are executed on the server and the result is returned to the browser as plain HTML PHP files have extensions ".php"

More about PHP

PHP is a server scripting language, an a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free and efficient alternatives to competitors such as Microsoft's ASP. PHP 7 is the latest stable release. This tutorial uses PHP 7.2.10.

Why PHP?

PHP runs on various platforms (Windows, Linux, Unix, Mac OS x, etc) PHP is compatible with almost all servers used today (Apache, IIS, etc) PHP supports a wide range of databases PHP is free. Download it from official PHP resource www.php.net PHP is easy to learn and runs efficiently on the server side

What does PHP statements end with?

PHP statements ends with: (:) - ends with semicolon

What is PHP?

PHP, Hypertext Preprocessor is a general purpose programming language originally designed for web development PHP is widely used, open source scripting language PHP scripts are executed on the server PHP is free to download and use

PHP The Global Keyword

The global keyword is used to access a global variable from within a function. <!DOCTYPE html> <html> <body> <?php $x = 5; $y = 10; function myTest() { global $x, $y; $y = $x + $y; } myTest(); // run function echo $y; // output the new value for variable $y ?> </body> </html> In general, you can not access variables in a function that are having global scope. In order to access them you have to use global term in front of it inside the function.

The PHP print statement

The print statement can be used with or without parantheses: print or print() <?php print "<h2>PHP is Fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?> <?php $txt1 = "Learn PHP"; $txt2 = "W3Schools.com"; $x = 5; $y = 4; print "<h2>" . $txt1 . "</h2>"; print "Study PHP at " . $txt2 . "<br>"; print $x + $y; ?>

PHP echo and print Statements

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.


Conjuntos de estudio relacionados

Y= 1/ 1-c1 * (c0 + I + G - c1T) - EQ NEL MKT DEI BENI -

View Set

Chapter 13 Recognizing Employee Contributions with Pay

View Set

Chapter 15 Quiz - Principles of Business, Marketing, & FInance

View Set

Status, Prestige, and Social Dominance

View Set

Lesson1 你好歌 Ni Hao Ge = Hello Song

View Set

PrepU CH 29: Drugs used to treat anemias

View Set

Safety and Infection Control Plan A quizzes

View Set