1. How to define a Variable
Use "$" to define a variable, different from JAVA, we don't need to use key word to define a variable, it's type is defined by the value we give. For example
$ a = "a";
$ A = 2; //these two variables are different, php is case sensitive
First, variables in PHP are case sensitive, and they can only start with A-Z or a-z or "_", such as $_abc, $Abc and $abC. Second, variables don't need key words to define them. For example:
<?php
$str ="I live in Montreal"; //string
$age = 24; //int
$status = true;//boolean
echo $str."<br>";
echo "I am ".$age."years old now";
?>
result:
2. Global variables
We can use key world "global" to define the global variable. But unlike Java, variable outside a bracket is the global vraible of the variable inside of the bracket. In PHP, we have to use "global" keyword to create the connection between variable outside of bracket and variable inside of the bracket.
<?php
$a = 1;
$b = 2;
function sum(){
global $a,$b;
$b=$a+$b;
}
sum();
echo $b;
?>
3. how to Connect PHP with MySql
<?php
$conn = @mysql_connect("localhost","root","111111") or die ("connection error!");
if($conn){
echo "good"."<br>";
}
mysql_select_db("test", $conn);
$sql = "select * from `test` ";
//mysql_query("set names 'GBK' ");
$query = mysql_query($sql,$conn);
while($rows = mysql_fetch_array($query)){
echo $rows[3]."<br>";
}
?>
System function mysql_connect() is used to connect mysql, and the mysql_select_db() is used to choose a database. mysql_query() will search the database with specific sql query.