PHP (1) Types and Variables

<?php //php code must be enclosed with <?php tags

// If your php fiel only contains PHP code, it is best
// practice to omit the php closing tag to prevent accidental ouput.

// Two forward slashes start a one-line comment.

# So will a hash but // is more common

/*
   surrounding text in slash-asterisk and asterisk-slash
   makes it a multi-line comment.
*/

// Use "echo" or "print"  to print output

print('Hello '); // prints "Hello " with no line break

echo "World\n"; // prints "World" with a line break

// Anything outside <?php tags is echoed automatically
?>

Hello World Again!

Types and Variables

  • Variables begin with the $ symbol.
  • A valid variable name starts with a letter or underscore, followed by any number of letters, or underscores.
  • Boolean values are case-insensitive
$boolean = true;  // or TRUE or True
$boolean = false; // or FALSE or False

Integers

$int1 = 12;
$int2 = -12;
$int3 = 012; // => 10 (a leading 0 denotes an octal number)
$int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal)
$int5 = 0b11111111 // => 255 (a leading 0b denotes a binary number)

Floats (aka doubles)

$float = 1.234;
$float = 1.2e3;
$float = 7E-10;

Delete Variable

unset($int1);

Arithmetic

$sum        = 1 + 1; // 2
$difference = 2 - 1; // 1
$product    = 2 * 2; // 4
$quotient   = 2 / 1; // 2

Shorthand Arithmetic

$number = 0;
$number += 1;      // Increment $number by 1
echo $number++;    // Prints 1 (increments after evaluation)
echo ++$number;    // Prints 3 (increments before evaluation)
$number /= $float; // Divide and assign the quotient to $numbe

String

// Strings should be enclosed in single quotes;
$sgl_quotes = '$String'; // => '$String'

// Avoid using double quotes except to embed other variables
$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'

// Special characters are only escaped in double quotes
$escaped   = "This contains a \t tab character.";
$unescaped = 'This just contains a slash and a t: \t';

// Enclose a variable in curly braces if needed
$apples = "I have {$number} apples to eat.";
$oranges = "I have ${number} oranges to eat.";
$money = "I have $${number} in the bank.";

// Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners
$nowdoc = <<<'END'
Multi line
string
END;

// Heredocs will do string interpolation
$heredoc = <<<END
Multi line
$sgl_quotes
END;

// String concatenation is done with .
echo 'This string ' . 'is concatenated';

// Strings can be passed in as parameters to echo
echo 'Multiple', 'Parameters', 'Valid';  // Returns 'MultipleParametersValid'

安装mysql

mysql

安装

docker pull mysql:latest
latest: Pulling from library/mysql
Digest: sha256:96edf37370df96d2a4ee1715cc5c7820a0ec6286551a927981ed50f0273d9b43
Status: Image is up to date for mysql:latest

现在运行mysql服务器

$ docker run  -d --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" mysql

docker inspect test-mysql 来查看ip地址和端口

现在用mysql客户端连接服务器

$ mysql -uroot -pqinjingfei -h 172.17.0.2 -P 3306
  • -u

user

  • -p

password

  • -P

port

  • -h

host

基本命令

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)


mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.01 sec)

mysql> select * from db;
...

PHP

PHP 是什么

PHP(“Hypertext Preprocessor”, 超文本预处理器)是一种被广泛应用的开放源代码的多用途脚本语言。

PHP能做什么

  • 收集表单数据
  • 生成动态网页
  • 发送/接受cookies

安装

sudo apt install php php-mysql

用PHP 连接 mysql

demo.php

<?php
        $dbhost = '172.17.0.2:3306';
        $dbuser = 'root';
        $dbpass = 'mypassword';
        $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
        if(! $conn ) {
           die('Could not connect: ' . mysql_error());
        }
        echo 'Connected successfully';
        mysqli_close($conn);
?>

执行

$ php demo.php
Connected successfully⏎