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

PHP 笔记一(systax/variables/echo/print/Data Type)的更多相关文章

  1. php中echo(),print(),print_r()之间的区别

    echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)  print只能打印出简单类型变量的值(如int,string)  print_r可以打印出复 ...

  2. php学习日志(3)-echo&print

    在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较. echo与print的区别:   echo print 连续输出字符串 能连续输出多个字符串 只能输出一个字符串 ...

  3. PHP中echo,print(),print_r()的区别

    echo是 php 语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)    print() 只能打印出简单类型变量的值(如int,string) print_r ...

  4. php中echo(),print(),print_r()用法

    原文 php中echo(),print(),print_r()用法 从我对echo(),print(),print_r()这个函数的理解是echo可输入字符串变量常量,print与echo差不多,但p ...

  5. echo() print() printf() print_r() 的区别

    echo是一个语言结构而非函数,因此它无法被变量函数调用, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print()    只能打印出简单类型变量的值(如int ...

  6. PHP里echo print print_r的区别

    echo ,print的区别在于echo 可以输出多个变量值,而print只有一个变量,做为一个字符串输出. 另一点区别在于echo 没有返回值,print有返回值1.print不能输出数组和对象. ...

  7. echo(),print(),print_r()之间的区别?

    echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)  print只能打印出简单类型变量的值(如int,string)  print_r可以打印出复 ...

  8. PHP中echo(),print(),print_r()之间的区别?

    echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print只能打印出简单类型变量的值(如int,string) print_r可以打印出复杂类 ...

  9. Linux C double linked for any data type

    /************************************************************************** * Linux C double linked ...

随机推荐

  1. php进程占用大量cpu优化

    使用TOP 命令发现php进程占用大量的cpu,达到100%,需要优化. 1 ll /proc/6264/fd 查看进程正在使用的资源 2 strace -p 6264 追踪进程正在做的事情 引用 h ...

  2. Thinking Of Matrix

    http://blog.163.com/bzm_square/blog/static/9355546320129582254842/ PS: 一种有关于矩阵的思维方法.....WiKi 向量空间,不定 ...

  3. crontab 管理指定用户的定时任务

    创建用户定时任务文件 touch /var/spool/cron/target_user crontab -u target_user /var/spool/cron/target_user 编辑用户 ...

  4. Python实现简单的记账本功能

    目标: 1.使用序列化cPickle 2.账户中钱要大于花费的钱,否则提示请存钱 2.编写函数,实现存钱,花钱,查询及退出功能 1.序列化 pickle是python实现序列化的模块,次模块存在使用C ...

  5. Nginx 安装

    1 编译环境 yum -y groupinstall "Development Tools" "Server Platform Development"   2 ...

  6. vim 命令加解密文件

    加密文件 vim file :X  (大写X,是加密 ,小写x是保存) 输入密码: 确认密码: 解除密码: vim file :set key= :wq 命令模式下,输入 /word 后回车,即查找w ...

  7. Docker的容器

    容器是一个打包了应用和服务的环境,是一个轻量级的虚拟机,每一个容器都由一组特定的应用和必要的依赖库组成. 容器的管理操作 容器常见的命令:查看.创建.启动.终止和删除 创建容器 docker crea ...

  8. javascript中的对象,原型,原型链和面向对象

    一.javascript中的属性.方法 1.首先,关于javascript中的函数/“方法”,说明两点: 1)如果访问的对象属性是一个函数,有些开发者容易认为该函数属于这个对象,因此把“属性访问”叫做 ...

  9. Nodes “-1” are listed in ADOP_VALID_NODES table but not in FND_NODES table

    While trying to apply patches to upgrade to 12.2.4, adop failed due to the below errors. Validating ...

  10. 【小窍门】cmd控制台无法输入中文(日文),输出非英文字符都是问号解决办法,中文都是问号解决办法

    在网上复制了一段代码,里面含有中文,而自己电脑本身系统是英文 win8/win10, 在VS 里debug之后输出后,中文都是问号.并不是乱码什么的. 奇怪了. 打开cmd,输入日文的时候,显示IME ...