PHP 笔记一(systax/variables/echo/print/Data Type)

PHP stands for "Hypertext Preprocessor" ,it is a server scripting language.

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 your database
  • PHP can be used to control user-access
  • PHP can encrypt data

1> Basic PHP Syntax

 <?php
// PHP code goes here
?>

2> Comments in PHP

 <?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;
?>

3> Case Sensitivity

In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive.

 <?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

However; all variable names are case-sensitive.

 <?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>

4> PHP Variables

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 is a Loosely Typed Language

PHP Variables Scope

PHP has three different variable scopes:

  • local
  • global
  • static

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

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

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

echo Display Text

 <?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.";
?>

echo Display Variables

 <?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 Display Text

 <?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>

print Display Variables

 <?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4; print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>

6> 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

Example

 <?php
$x = "Hello world!";
$y = 5985;
$z = 10.365;
$cars = array("Volvo","BMW","Toyota");
// var_dump() function returns the data type and value
var_dump($cars);
8 $a = null;

Object Example

 <?php
class Car {
function Car() {
$this->model = "VW";
}
} // create an object
$herbie = new Car(); // show object properties
echo $herbie->model;
?>
上一篇:python中的内存管理


下一篇:JAVA中for与while关于内存的细节问题