owned this note
owned this note
Published
Linked with GitHub
PHP
===
參考:
1.http://php.net/manual/en/index.php
2.PHP,MYSQL,JAVASCRIPT 學習手冊
先照著laravel教學安裝環境
https://laravel.com/docs/5.4
# install
Server Requirements
The Laravel framework has a few system requirements. Of course, all of these requirements are satisfied by the Laravel Homestead virtual machine, so it's highly recommended that you use Homestead as your local Laravel development environment.
However, if you are not using Homestead, you will need to make sure your server meets the following requirements:
PHP >= 5.6.4
OpenSSL PHP Extension
PDO PHP Extension
Mbstring PHP Extension
Tokenizer PHP Extension
XML PHP Extension
install vargant
---
通常要使用laravel需要安裝上面那堆東西,不過通過 homestead 這個設定好的環境,可以免去這些步驟。
---
不過往下讀,好像發現另一個東西更方便,叫做valet。
https://laravel.com/docs/5.4/valet
我們先安裝php 再安裝 composer 再來安裝 valet
```
brew update
```
遇到問題
```
Error: /usr/local is not writable. You should change the ownership
and permissions of /usr/local back to your user account:
sudo chown -R $(whoami) /usr/local
```
跟著做
```
sudo chown -R $(whoami) /usr/local
```
然後就可以更新了
再來要裝php
```
brew install homebrew/php/php71
```
裝完之後來裝composer
https://getcomposer.org/download/
```
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
```
---
This installer script will simply check some php.ini settings, warn you if they are set incorrectly, and then download the latest composer.phar in the current directory. The 4 lines above will, in order:
Download the installer to the current directory
Verify the installer SHA-384 which you can also cross-check here
Run the installer
Remove the installer
WARNING: Please do not redistribute the install code. It will change with every version of the installer. Instead, please link to this page or check how to install Composer programmatically.
---
裝完composer後
將composer 加到全域環境
mv composer.phar /usr/local/bin/composer
composer 是透過在 composer.json 中 require 套件來進行管理
```
composer global require laravel/valet
```
```
valet install
```
[Laravel “valet install” not found](http://stackoverflow.com/questions/37085102/laravel-valet-install-not-found)
solution:
echo $PATH
add
export PATH=$PATH:~/.composer/vendor/bin
to ~/.bash_profile
```
Last login: Wed Apr 26 14:06:55 on ttys002
dujiahaodeMacBook-Pro:~ Eric$ echo $PATH
/Users/Eric/.nvm/versions/node/v6.2.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/Eric/.composer/vendor/bin
dujiahaodeMacBook-Pro:~ Eric$ valet install
Password:
[nginx] is not installed, installing it now via Brew... 🍻
[dnsmasq] is not installed, installing it now via Brew... 🍻
Valet installed successfully!
```
Once Valet is installed, try pinging any *.dev domain on your terminal using a command such as ping foobar.dev. If Valet is installed correctly you should see this domain responding on 127.0.0.1.
ping看看是否成功
成功後我們安裝symfony
```
sudo mkdir -p /usr/local/bin
sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony
```
安裝完建立第一個專案
$ symfony new hello

先用 valet park 建立環境
訪問 http://hello.dev
成功!
---
關於 valet park valet link
Serving Sites
Once Valet is installed, you're ready to start serving sites. Valet provides two commands to help you serve your Laravel sites: park and link.
The park Command
Create a new directory on your Mac by running something like mkdir ~/Sites. Next, cd ~/Sites and run valet park. This command will register your current working directory as a path that Valet should search for sites.
Next, create a new Laravel site within this directory: laravel new blog.
Open http://blog.dev in your browser.
That's all there is to it. Now, any Laravel project you create within your "parked" directory will automatically be served using the http://folder-name.dev convention.
The link Command
The link command may also be used to serve your Laravel sites. This command is useful if you want to serve a single site in a directory and not the entire directory.
To use the command, navigate to one of your projects and run valet link app-name in your terminal. Valet will create a symbolic link in ~/.valet/Sites which points to your current working directory.
After running the link command, you can access the site in your browser at http://app-name.dev.
To see a listing of all of your linked directories, run the valet links command. You may use valet unlink app-name to destroy the symbolic link.
---
[vargant](https://www.vagrantup.com/intro/getting-started/index.html) 有空再來研究。
# 亂玩一通
首先使用 Interactive shell
在terminal 輸入 php -a
>echo "hi"
怎麼沒反應?
記得加 ";"
>echo "hi";
>hi
>
# coding style
http://symfony.com/doc/current/contributing/code/standards.html
## Coding Standards
3.2 version
edit this page
When contributing code to Symfony, you must follow its coding standards. To make a long story short, here is the golden rule: Imitate the existing Symfony code. Most open-source Bundles and libraries used by Symfony also follow the same guidelines, and you should too.
Remember that the main advantage of standards is that every piece of code looks and feels familiar, it's not about this or that being more readable.
Symfony follows the standards defined in the PSR-0, PSR-1, PSR-2 and PSR-4 documents.
這些規範都是為了統一各個框架各自造輪子的窘境。
---
## Structure
Add a single space after each comma delimiter;
每個分號後面都加一個空白
Add a single space around binary operators (==, &&, ...), with the exception of the concatenation (.) operator;
邏輯符號後面也要加空白
Place unary operators (!, --, ...) adjacent to the affected variable;
Always use identical comparison unless you need type juggling;
Use Yoda conditions when checking a variable against an expression to avoid an accidental assignment inside the condition statement (this applies to ==, !=, ===, and !==);
Add a comma after each array item in a multi-line array, even after the last one;
陣列中每個元素後面都要加逗號,最後一個也要加。
Add a blank line before return statements, unless the return is alone inside a statement-group (like an if statement);
再回傳值之前先空一行,除非是單獨存在一個區塊中。
Use return null; when a function explicitly returns null values and use return; when the function returns void values;
Use braces to indicate control structure body regardless of the number of statements it contains;
Define one class per file - this does not apply to private helper classes that are not intended to be instantiated from the outside and thus are not concerned by the PSR-0 and PSR-4 autoload standards;
Declare the class inheritance and all the implemented interfaces on the same line as the class name;
Declare class properties before methods;
Declare public methods first, then protected ones and finally private ones. The exceptions to this rule are the class constructor and the setUp() and tearDown() methods of PHPUnit tests, which must always be the first methods to increase readability;
Declare all the arguments on the same line as the method/function name, no matter how many arguments there are;
Use parentheses when instantiating classes regardless of the number of arguments the constructor has;
Exception and error message strings must be concatenated using sprintf;
Calls to trigger_error with type E_USER_DEPRECATED must be switched to opt-in via @ operator. Read more at Deprecations;
Do not use else, elseif, break after if and case conditions which return or throw something;
Do not use spaces around [ offset accessor and before ] offset accessor.
## Naming Conventions
Use camelCase, not underscores, for variable, function and method names, arguments;
Use underscores for option names and parameter names;
Use namespaces for all classes;
Prefix abstract classes with Abstract. Please note some early Symfony classes do not follow this convention and have not been renamed for backward compatibility reasons. However all new abstract classes must follow this naming convention;
Suffix interfaces with Interface;
Suffix traits with Trait;
Suffix exceptions with Exception;
Use alphanumeric characters and underscores for file names;
For type-hinting in PHPDocs and casting, use bool (instead of boolean or Boolean), int (instead of integer), float (instead of double or real);
Don't forget to look at the more verbose Conventions document for more subjective naming considerations.
Service Naming Conventions¶
A service name contains groups, separated by dots;
The DI alias of the bundle is the first group (e.g. fos_user);
Use lowercase letters for service and parameter names;
A group name uses the underscore notation.
Documentation¶
Add PHPDoc blocks for all classes, methods, and functions;
Group annotations together so that annotations of the same type immediately follow each other, and annotations of a different type are separated by a single blank line;
Omit the @return tag if the method does not return anything;
The @package and @subpackage annotations are not used.
License¶
Symfony is released under the MIT license, and the license block has to be present at the top of every PHP file, before the namespace.
# 參考
變數命名規則:
第一個字必須是字母貨_
變數名稱只能是英數_
變數名聲不能有空格,如有兩字元以上請用_
有大小寫之分
http://oomusou.io/php/php-psr2/
## 邏輯運算
&& 高優先
and 低優先
||
or
!
xor
字串串接
使用句點進行字串串接
$megs = 5;
echo "You have".$megs."messages";
out put :
You have 5 messages
字串類型
$info = 'this is a $variable';
使用單引號 那 this is a $variable 整句都是變數
如果使用雙引號 $variavle 會被視為變數 就會傳值進去
若字串中有特殊字元 需用反斜線 \ 處理 而這種方法只適用在雙引號中的字串
echo多行的時候,可用 "" 包起來
或者用
echo<<<_END
_END;
值得注意的是結束的標籤定要在新一行的開頭,且後面不得加任何東西,連註解也不行。
## 變數的轉型
```php=
<?php
$number = 12345 * 67890;
echo substr($number, 3, 1);
?>
```
這段的結果是1
---
```php=
<?php
$number = "12345 * 67890";
echo substr($number, 3, 1);
?>
```
這段的結果是4,因為把它當成字串來擷取 第四個元素就是4
---
## 常數
兩件事:
常數前面不用加$
只能用define來定義
通常使用大寫來定義
```php=
drfine("ROOT_LOCATION", "/usr/local/www/");
$directory = ROOT_LOCATION;
```
奇奇怪怪的常數:
```php=
_LINE_
_FILE_
_DIR_
_FUNCTION_
_CLASS_
_METHOD_
_NAMESPACE_
//example:
ehco "this is line " . _LINE_ . " of file " . _FILE_;
```
## FUNCTION
```php=
<?php
function longdate($timestamp)
{
return date("l F jS Y", $timestamp);
}
echo longdate(time());
?>
```
變數範圍:
如果只想要變數只在函式內作用的話,
可以使用 $temp 。
區域變數:
來比較一下結果
```php=
<?php
function longdate($timestamp)
{
$temp = date("l F jS Y", $timestamp);
return "the date is $date";
}
?>
```
```php=
<?php
$temp = "the date is ";
echo longdate(time());
function longdate($timestamp)
{
return $temp . date("l F jS Y", $timestamp);
}
?>
```
第一種當呼叫完之後,變數就在回傳完清除。
第二種longdate無法存取,因為 $temp 不宣告在裡面。
怎麼解決第二種的問題呢?一種是傳遞出來一種是傳遞進去。
```php=
<?php
$temp = "the date is ";
echo $temp . longdate(time());
function longdate($timestamp)
{
return date("l F jS Y", $timestamp);
}
?>
```
```php=
<?php
$temp = "the date is ";
echo longdate($temp, time());
function longdate($text, $timestamp)
{
return $text . date("l F jS Y", $timestamp);
}
?>
```
global variable:
```php=
global $is_logged_in;
```
recommend using capital letters
```php=
global $IS_LOGGED_IN;
```
static variable:
```php=
<?php
function test()
{
static $count = 0;
echo $count;
$count++;
}
?>
```
leagal declare & illegal declare:
宣告靜態變數時只能指定初始值,並不能用算式去賦值。
```php=
<?php
static $int = 0; //leagal
static $int = 1+2; //illegal (Parse error)
static $int = sqrt(144); //illegal
?>
```
# Predefined Variables
Superglobals — Superglobals are built-in variables that are always available in all scopes
$GLOBALS — References all variables available in global scope
$_SERVER — Server and execution environment information
$_GET — HTTP GET variables
$_POST — HTTP POST variables
$_FILES — HTTP File Upload variables
$_REQUEST — HTTP Request variables
$_SESSION — Session variables
$_ENV — Environment variables
$_COOKIE — HTTP Cookies
$php_errormsg — The previous error message
$HTTP_RAW_POST_DATA — Raw POST data
$http_response_header — HTTP response headers
$argc — The number of arguments passed to script
$argv — Array of arguments passed to script
超級全域變數的安全性:
```=php
$came_from = $_SERVER['HTTP_REFERER'];
```
為了安全,使用這類變數之前最好使用PHP提供的htmlentities(),他會將所有的字元轉換成html實體,例如>,< 轉換成<,> 字串。
因此較佳的$_SERVER存取方式為:
```php=
$came_from = htmlentities($_SERVER['HTTP_REFERER']);
```
## 除了超全域變數之外,任何需要經過處理來輸出使用者或第三方資料都要使用htmlentities來處理!