Variables and Types

Posted yangbocsu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Variables and Types相关的知识,希望对你有一定的参考价值。

Variables and Types


To define a variable, simply use the following syntax:

$x = 1;
$y = "foo";
$z = True;

We have just defined a variable named x with the number 1, a variable named y with the string "foo" and a variable name z with the boolean value True. Once they are defined, we can use them in the code.

 

php has many types of variables, but the most basic variable types are integer (whole numbers), float (real numbers), strings, and booleans.

PHP also has arrays and objects which we will explain in other tutorials.

 

Variables can also be set to NULL, which means that the variables exist, but do not contain any value.

 

Operators

We can use simple arithmetic operators to add, subtract or concatenate between variables.

We can also print out PHP variables using the echo command (you can try it out now).

For example, let's sum up two numbers, put the result in a new variable, and print out the result.

$x = 1;
$y = 2;
$sum = $x + $y;
echo $sum;       // prints out 3

 

String formatting

Like Perl, PHP double quoted strings can format strings using defined variables. For example:

$name = "Jake";
echo "Your name is $name";    // prints out Your name is Jake

Exercise

Part 1

Define the variables name and age so that a line would be printed out saying the following sentence:

Hello Jake. You are 20 years old.

 

Notice that the code contains a special character sequence at the end called a newline - \\n. This sequence will cause the next line printed out to be printed out on the next line. For web development, this is not important, since we use html tags for this purpose.

 

Part 2

Sum up the variables x and y and put the result in the sum variable.

 

<?php
// Part 1: add the name and age variables.
$name = "Jake";
$age = 20;
echo "Hello $name. You are $age years old.\\n";

// Part 2: sum up the variables x and y and
// put the result in the sum variable.
$x = 195793;
$y = 256836;
$sum = NULL;
$sum = $x + $y;
echo "The sum of $x and $y is $sum."
?>

 

 

以上是关于Variables and Types的主要内容,如果未能解决你的问题,请参考以下文章

Python学习19:函数和变量 Function and variables

fitnesse - Variables and Symbols

tf.trainable_variables() and tf.all_variables()

T-SQL Recipes之 Table Variables and Temporary Tables and CTE

3.Discrete Random Variables and Probability Distributions

Mutable and Immutable Variables in Python