PHP Learning Notes-Basics (1)

Source: Internet
Author: User

1, the PHP script executes on the server, and then sends back the pure HTML result to the browser.

2. Basic PHP syntax

  1) The PHP script can be placed anywhere in the document.

2) PHP script begins with <?php and ends with ?> .

3) The default file name extension for PHP files is ". php".

4) PHP files usually contain HTML tags and some php script code.

5) PHP supports three types of annotations:

This is a single-line comment
# This is also a single-line comment
/*
This is a multiline comment block.
It spans the
MultiRow
*/

  6) The PHP statement ends with a semicolon (;). The close tag of a PHP block also automatically indicates a semicolon (so you don't have to use a semicolon in the last line of the PHP block).

3, PHP case sensitive

1) in PHP, all user-defined functions, classes, and keywords (such as if, else, Echo, and so on) are not case sensitive.

2) All variables are case sensitive.

3) constants can be defined as case sensitive or insensitive:

Define ("Constant name", "Constant Value", "false/true");//default to False the case is not sensitive, and true is case sensitive.

4. PHP Data type

PHP has a data type, but you don't need to declare a data type when you define a variable

  string: is a sequence of characters, such as "Hello world!". The string can be any text within the quotation marks, either single or double quotation marks.

integer: An integer is a number that has no decimals.

Integer rule:

    • Integers must have at least one number (0-9)
    • Integers cannot contain commas or spaces
    • Integers cannot have decimal points
    • Integers can be both positive and negative
    • Integers can be specified in three formats: decimal, hexadecimal (prefix 0x), or octal (prefix 0)

eg

  <?php
  $x = 5985;
  var_dump($x);//返回变量的数据类型和值
  $x = -345; // 负数
  var_dump($x);
  $x = 0x8C; // 十六进制数
  var_dump($x);
  $x = 047; // 八进制数
  var_dump($x);
  ?>

Run Result: Int (5985) int ( -345) int (+) int (39)

Floating-point number : floating-point numbers are digits in the form of a decimal point or exponent.

eg

   <?php
  $x = 10.365;
  var_dump($x);
  $x = 2.4e3;//覆盖了之前$x的值
  var_dump($x);
  $x = 8E-5;
  var_dump($x);
  ?>

Operating Result: float (10.365) float (2400) float (8.0E-5)

logic:true or false.

Arrays: Arrays Store multiple values in a variable.

Object : An object is a data type that stores data and information about how the data is processed. (Instance of Class)

In PHP, you must explicitly declare an object.

First we must declare the class of the object. Use the class keyword. A class is a structure that contains properties and methods.

We then define the data type in the object class, and then use this data type in an instance of the class:

Declaration of the class:

Class Car
{
var $color;
function Car ($color = "green") {
$this->color = $color;
}
function What_color () {
return $this->color;
}
}

function Print_vars ($obj) {
foreach (Get_object_vars ($obj) as $prop = = $val) {
echo "\t$prop = $val \ n";
}
}


$herbie = new Car ("white");//Instantiate a class, $herbie is an object
echo "\herbie:properties\n";
Print_vars ($herbie);//Call the Print_vars method to output the corresponding value

Run Result: \herbie:properties color = White

null: A special null value indicates that the variable has no value. Null is the only possible value for the data type NULL.

The null value indicates whether the variable is empty. Also used to differentiate between empty strings and null-valued databases.

You can empty the variable by setting the value to NULL:

5. Variables1) PHP variable rules:
    • The variable starts with the $ sign, followed by the name of the variable
    • Variable names must begin with a letter or underscore
    • Variable names cannot start with a number
    • Variable names can contain only alphanumeric characters and underscores (A-Z, 0-9, and _)
    • Variable names are case sensitive ($y and $Y are two different variables)

Note: The PHP variable name is case-sensitive!

2) PHP does not have a command to create a variable. Variables are created the first time they are assigned a value. Eg: $y = 5;

3) PHP is a loosely typed language, and we don't have to tell the data type of the PHP variable. PHP automatically converts the variable to the correct data type based on its value.

4) PHP variable scope

 In PHP, variables can be declared anywhere in the script.

The scope of a variable refers to the part of the script that a variable can be referenced/used.

PHP has three different scope of variables:

    • Local: Variables declared inside a function have a local scope and can only be accessed inside the function.
    • Global: Variables declared outside the function have global scope and can only be accessed outside of the function.
    • Static (statically)

Note: You can create a local variable with the same name in a different function, because a local variable can only be recognized by the function in which it was created.

6) PHP Global keywords

 The global keyword is used to access variables within the function.

To do this, use the Global keyword before (inside the function) variable:

<?php
$x = 5;
$y = 10;

function MyTest () {
Global $x, $y;//due to the use of the GLOBALS keyword, the delegate accesses the $x, $y

$y = $x + $y;
}
MyTest ();
Echo $y; //Output 15
?>
  PHP also stores all global variables in an array named $GLOBALS [index]. The subscript contains the variable name. This array is also accessible within the function and can be used to update global variables directly.

    <?php
$x = 5;
$y = 10;

function MyTest () {
$GLOBALS [' y ']= $GLOBALS [' x ']+ $GLOBALS [' Y '];//all global variables are stored in an array of $globals[' global variable name ']
}

MyTest ();
Echo $y; //Output 15
?>

7) PHP static keyword

  Typically, all variables are deleted when the function finishes/executes. However, sometimes I need to not delete a local variable . Achieving this requires a bit of further work.

To do this, use the static keyword when you first declare the variable.

Whenever a function is called, the information stored by the variable is the information contained in the last Call of the function.

Note: The variable is still a local variable of the function.

6. Constants

A constant is an identifier (name) for a single value. The value cannot be changed in the script.

Valid constant names begin with a character or underscore (there is no $ symbol in front of the constant name).

Note: Unlike variables, constants are automatically global throughout the script.

7. Two basic output methods: Echo and Print.

The difference between Echo and print:

    • echo-capable of outputting more than one string
    • Print-only one string is output and always returns 1

Tip: Echo is slightly faster than print because it does not return any values.

eg

<?php

$txt 1= "Learn PHP";

$cars =array ("Volvo", "BMW", "SAAB");


echo "echo "Hello world!<br>";//Output a string
echo "This", "string", "is", "made", "with multiple parameters."; /Can output multiple strings

echo $txt 1;//output variable

echo "My car is a {$cars [0]}";

Print "I ' m about to learn php!";
Print $txt 1;

Print "My car is a {$cars [0]}";
?>

8. Operators

1) PHP Arithmetic operators: + (plus),-(minus), * (multiply),/(except),% (remainder)

2) PHP assignment operators: =, + =,-=, *=,/=,%=

3) PHP string operator:. (threaded, $txt 1 = "Hello" $txt 2 = $txt 1. "world!"),. = (string assignment, $txt 2 = "Hello" $txt 2. = "world!" )。 Two x $txt2== "Hello world!" ”

4) PHP increment/decrement operator: + + $x, $x + + 、--$x, $x--

5) PHP comparison operator: = = (equals), = = = (all equals (exactly), if both sides of the comparison are equal, and they are of the same type, return true),! =, <>,!== (not congruent (completely different)), >, <, >=, <=

6) PHP logical operators: And, OR, XOR, &&, | |,!

7) PHP Array operators: +, = =, = =,! =, <>,! ==

eg

<?php
$x = Array ("A" = "red", "b" = "green");
$y = Array ("c" = "Blue", "d" = "yellow");
$z = $x + $y; //$x union with the $y
Var_dump ($Z);
Var_dump ($x = = $y);
Var_dump ($x = = = $y);
Var_dump ($x! = $y);
Var_dump ($x <> $y);
Var_dump ($x!== $y);
?>


Operation Result:

Array (4) {["a"]=> string (3) "Red" ["B"]=> string (5) "Green" ["C"]=> string (4) "Blue" ["D"]=> string (6 ) "Yellow"}
BOOL (FALSE)
BOOL (FALSE)
BOOL (TRUE)
BOOL (TRUE)

BOOL (TRUE)

PHP Learning Notes-Basics (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.