PHP(1): Variables and Operaters in PHP

本文介绍了PHP中如何定义变量及变量的特点,并展示了如何通过全局关键字使用函数内外的变量。此外,还提供了连接MySQL数据库并执行查询的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值