Table of Contents

Programming Languages

PHP



References

Documentation

Install

Ubuntu

apt install php-fpm php7.4-sqlite3 php7.4-mbstring

Module

php -m   <- check module
# ln -s /opt/remi/php56/root/usr/lib64/php/modules/sqlite3.so /usr/lib64/php/modules/sqlite3.so


Configuration (php.ini)

Securing PHP

Configuration Note
expose_php = Off Don't display php version

Performance Directives

Configuration Note
upload_max_filesize = 20M
max_execution_time = 30 Max execution tie for each script in seconds.
memory_limit = 128M Max amount of memory a script may use.

Libraries


Framework

Framework Note
CodeIgniter http://www.codeigniter.com/
FuelPHP http://fuelphp.com/
CakePHP http://cakephp.org/
Laravel http://laravel.com/




Grammar

print

<?php
print "Hello World!";
echo "Hello World!";

<?php
$array = array(1,2,3);
var_dump($array);
 
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

commnent

// aaaaa
# aaaaa

/*
aaaaaaa
bbbbbbb
*/


Operator

$a . $b

$a + $b
$a - $b
$a * $b
$a / $b
$a % $b

$a == $b
$a === $b

$a != $b
$a <> $b
$a !== $b

$a < $b
$a > $b
$a <= $b
$a >= $b

$a and $b
$a && $b


$a or $b
$a || $b

!$a          //$a is  false

$a xor $b


Variables

$a = 'string';
$i = 1;

<?php
$int = 100;
$float = 100.123;

<?php
$i = 1 + 1;
$i = 1 - 1;
$i = 1 * 1;
$i = 1 / 2;

<?php
$div = intval(3 / 2);
$mod = 3 % 2; 

<?php
$i++;
$i--; 

<?php
$str1 = "abc\tcde"; // abc  cde
$str2 = 'abc\tcde'; // abc\tcde
 
$str3 = "$str1 100"  // abc  cde 100
$str4 = "{$str1}100"

<?php
$join1 = 'aaa' . 'bbb';
$join2 = implode(',', array('aaa', 'bbb', 'ccc'));
 

$split = explode(',', 'aaa,bbb,ccc');
 

$length = strlen('abcdef');

// mb_internal_encoding('UTF-8'); 
$mb_length = mb_strlen('aiueo');

$substr = substr('abcd', 0, 2); // ab
 
// search
$index = strpos('abcd', 'bc');

$_SERVER['REMOTE_ADDR']

<?php
//test.php?id=10&group=test
echo $_GET['id'];
echo $_GET['group'];

<?php
/*
<form method="POST" action="test.php">
  <input type="text" name="key" value="ABC">
  <input type="text" name="page" value="3">
  <input type="submit" value="OK">
</form>
*/
echo $_POST['key'];


Array

<?php
$array1 = array(1, 2, 3);/
$array2 = array('a' => 1,  'b' => 2, 'c' => 3);
$array3 = array(1, 'a' => 1, 2);

<?php
$i = $array1[0];
$s = $array2['a'];

<?php
$array1[3] = 1;
$array2['z'] = 'zzz';


Conditionals

Example Name Result
a == b Equal true if $a is equal to $b
a === b Identical true if $a is equal to $b, and they are of the same type.
a != b Not equal true if $a is not equal to $b
a <> b Not equal true if $a is not equal to $b
a !== b Not identical true if $a is not equal to $b, or they are not of the same type.
a > b Greater than true if $a is strictly greater than $b.
a >= b Greater than or equal to true if $a is greater than or equal to $b.
a < b Less than true if $a is strictly less than $b.
a <= b Less than or equal to true if $a is less than or equal to $b.

if

<?php
if ($var == xxxx) {
}

if ($sample > 5) {
}

<?php if (condition): ?>
  <span>hoge</span>
<?php endif; ?>

if else

<?php
if (!empty($value)) {
} else {
}

<?php if (condition): ?>
  <span>hoge</span>
<?php else: ?>
  <span>foo</span>
<?php endif; ?>

if - else if

<?php
if (condition) {
} else if {
}

<?php if (condition): ?>
  <span>hoge</span>
<?php elseif (condition): ?>
  <span>foo</span>
<?php endif; ?>

Ternary operator

if($cond===true){ func1(); }else{ func2(); }
$cond===true ? func1() : func2();

while

<?php
$i = 0;
while ($i < 5) {
  // 
  $i++;
}

<?php while ($i < 5): ?>
  <span><?php echo htmlspecialchars($i); ?></span>
  <?php $i++; ?>
<?php endwhile; ?>

for

<?php
for ($i = 0 ; $i < 5 ;$i++) {
}

<?php for ($i = 0 ; $i < 5 ; $i++): ?>
  <span><?php echo htmlspecialchars($i); ?></span>
<?php endfor; ?>

foreach

<?php
foreach ($array as $v) {
  // $v : value
}
foreach ($array as $k => $v) {
  // $k : Key 、$v : value
}

<?php foreach ($array as $v): ?>
  <span><?php echo htmlspecialchars($v); ?></span>
<?php endforeach; ?>

switch


<?php
$day = "Sun";
switch ($day) {
    case "Sun":
        echo "Sunday\n";
        break;
    case "Mon":
        echo "Monday\n";
        break;
    case "Tue":
        echo "Tuesday\n";
        break;
    default:
        echo "Unknown\n";
        break;
}


Include

<?php
require 'sample.php';

<?php
require_once 'sample.php';

<?php
include 'common.php';

<?php
include_once 'sample.php';


subroutine

<?php
function sum($v1, $v2) {
  return $v1 + $v2;
}
$total = sum(1, 2); // $total = 3
 
function get_multi($v1, $v2) {
  $v1 += 100;
  $v2 += 200;
  return array($v1, $v2);
}
 
list($ret1, $ret2) = get_multi(1, 2); // $ret1 = 101 / $ret2 = 202


File

fopen

<?php
// Read
$fp = fopen("/path/to/file", "r");
if (!is_resource($fp)) {
  die("can't open file");
}
 
while (!feof($fp)) {
  $line = fgets($fp, 4096);
  //
}
fclose($fp);
 
// Write
$fp = fopen("/path/to/file", "w");
if (!is_resource($fp)) {
  die("can't open file");
}
 
fputs($fp, $buff);
fclose($fp);

file

<?php
$list = file("/path/to/file");

file_get_contents / file_put_contents

<?php
// read
$contents = file_get_contents("/path/to/file");
 
// write
file_put_contents("/path/to/file", $buff); 


Check

empty

if (empty($host)) {
  $ErrFlg = 1;
  $ERRMSG = $ERRMSG . "Your host or IP is empty.<br>";
}

checking only character

if (!preg_match("/^[a-zA-Z]+$/", $record)) {
  $ErrFlg = 1;
}

only number

if(!preg_match("/^[0-9]+$/",$num)){
    $ErrFlg = 1;
}

checking number of character

if (mb_strlen($host) > 10) {
  $ErrFlg = 1;
  $ERRMSG = $ERRMSG . "Your number of character is too wordy.<br>";
}

check IP

<?php
$IP = "123.45.67.89";

if(preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/', $IP)){
  echo("$IP is IP address.");
}else{
  echo("$IP is not IP address.");
}

check FQDN

if(!preg_match('/^([A-Za-z0-9][A-Za-z0-9\-]{1,61}[A-Za-z0-9]\.)+[A-Za-z]+$/', $host)){
  $ErrFlg = 1;
  $ERRMSG = $ERRMSG . "Your FQDN is bad.<br>";
}

check mail address

if (!preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)$/' , $mail)) {
   $ErrFla = 1;
}