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;
?>
PHP 笔记一(systax/variables/echo/print/Data Type)的更多相关文章
- php中echo(),print(),print_r()之间的区别
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print只能打印出简单类型变量的值(如int,string) print_r可以打印出复 ...
- php学习日志(3)-echo&print
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较. echo与print的区别: echo print 连续输出字符串 能连续输出多个字符串 只能输出一个字符串 ...
- PHP中echo,print(),print_r()的区别
echo是 php 语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print() 只能打印出简单类型变量的值(如int,string) print_r ...
- php中echo(),print(),print_r()用法
原文 php中echo(),print(),print_r()用法 从我对echo(),print(),print_r()这个函数的理解是echo可输入字符串变量常量,print与echo差不多,但p ...
- echo() print() printf() print_r() 的区别
echo是一个语言结构而非函数,因此它无法被变量函数调用, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print() 只能打印出简单类型变量的值(如int ...
- PHP里echo print print_r的区别
echo ,print的区别在于echo 可以输出多个变量值,而print只有一个变量,做为一个字符串输出. 另一点区别在于echo 没有返回值,print有返回值1.print不能输出数组和对象. ...
- echo(),print(),print_r()之间的区别?
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print只能打印出简单类型变量的值(如int,string) print_r可以打印出复 ...
- PHP中echo(),print(),print_r()之间的区别?
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print只能打印出简单类型变量的值(如int,string) print_r可以打印出复杂类 ...
- Linux C double linked for any data type
/************************************************************************** * Linux C double linked ...
随机推荐
- webdriver零碎知识点
#零碎知识点,用于记录平时遇到的比较杂的知识点 driver.current_url 获取当前url phantomjs 实现无浏览器界面自动化测试(driver = webdriver.Phanto ...
- C++ Windows 下 根据进程名获取进程ID 以及该进程下所有窗口的句柄
#include <windows.h> #include <stdint.h> #include <tlhelp32.h> #include <stdio. ...
- .NET中MemCached使用介绍
阅读目录 1.MemCached是什么? 2.Window中MemCached安装 3.MemCached命令 4.简单示例 MemCached是什么 MemCached是一个自由开源,高性能,分布式 ...
- 设置默认访问项目的客户端的浏览器版本(IE版本)
在项目开发部署中,发现浏览器不兼容现象,在不处理兼容性情况下让用户更好体验(IE浏览器) 我们来设置客户端默认访问项目的浏览器版本 如下所示的是不同IE版本下的效果截图比较: IE5.IE6下: IE ...
- js弹出框,禁刷新
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Chrome 中的 JavaScript 断点设置和调试技巧
Console:此功能是模拟js控制台,直接写代码,查看结果.高级功能使用时开启断点,查看变量的变化过程.还可以条用函数. Resources:次功能是查看加载页面所用的资源,链接的数据库,域名下保存 ...
- android 屏幕分辨率 更改
手头上有一个320x240的LCD.运行android时,显示内容过大,需要更改屏幕的分辨率. 参考链接 http://www.bkjia.com/Androidjc/899396.html http ...
- 高级java必会系列一:多线程的简单使用
众所周知,开启线程2种方法:第一是实现Runable接口,第二继承Thread类.(当然内部类也算...)常用的,这里就不再赘述.本章主要分析总结线程池和常用调度类. 一.线程池 1.newCache ...
- vim - Convert between hex and decimal
http://vim.wikia.com/wiki/VimTip448 ga g8
- Java 实现函数回调
在Java里没用委托(delegate)这方法,所以想要实现回调还是有些麻烦.(想了解C#如何实现?请查看:http://www.cnblogs.com/Martin_Q/p/4478494.html ...