분류 php

내 사랑하는 PHP 치트 시트

컨텐츠 정보

  • 조회 350 (작성일 )

본문

다음은 학습 여정에서 만든 치트 시트입니다. 권장 사항 (더하기 / 빼기)이 있으면 알려주세요.


명명 규칙


<?php

$firstName = 'Mike'  // camelCase
function updateProduct() // camelCase
class ProductItem // StudlyCaps
const ACCESS_KEY = '123abc'; // all upper case with underscore separators


Output


echo 'Hello World';

// Debug output
var_dump($names);
print_r($products);


변수 선언


$name = 'Mike'; //string
$isActive = true; //boolean
$number = 25; //integer
$amount = 99.95; //float


Strings


//string concat
echo 'Hello ' . $name;

//string escape characters \n new line  \t tab  \\ backslash
echo "Hello Mike\nHello David";

//string interpolation
echo "Hello $name";

//string number of characters
echo strlen($name);

// Remove space(s) before and after
echo trim($text)

// Convert to lowercase / uppercase
echo strtolower($email);
echo strtoupper($name);

// Converts the first character to uppercase
echo ucfirst($name);  // 'Mike' 

// Replace text a by text b in $text
echo str_replace('a', 'b', $text);

// String Contains (PHP 8)
echo str_contains($name, 'ke')  # true


Numbers


// Check if numeric
echo is_numeric('59.99'); # true

// Round a number
echo(round(0.80));  // returns 1
echo(round(0.49));  // returns 0

// Random number
echo(rand(10, 100)); # 89


조건부


// Ternary operator (true : false)
echo $isValid ? 'user valid' : 'user not valid';

//Null Coalesce Operator
echo $name ?? 'Mike';  //output 'Mike' if $name is null

//Null Coalesce Assignment
$name ??= 'Mike';

// Null Safe Operator (PHP 8) will return null if one ? is null
echo $user?->profile?->activate();

// Null Safe + Null Coalesce (if null will return 'No user profile')
echo $user?->profile?->activate() ?? 'Not applicable';

//Spaceship operator return -1 0 1
$names = ['Mike', 'Paul', 'John']
usort($names, function($a, $b) {
    return $a <=> $b;
}
// ['John', 'Mike', 'Paul']

// Return false when convert as boolean
false, 0, 0.0, null, unset, '0', '', []

//Conditionals
if ($condition == 10) {
    echo 'condition 10'
} elseif  ($condition == 5) {
    echo 'condition 5'
} else {
    echo 'all other conditions'
}

//Comparaison operation
== // equal no type check
=== // equal with type check
!= //not equal
|| //or
&& //and
> //greater than
< //less than


// Match Expression (PHP 8)
$type = match($color) {
    'red' => 'danger',
    'yellow', 'orange' => 'warning',
    'green' => 'success',
    default => 'Unknown'
};


루프 및 반복


//for loop
for ($i = 0; $i < 20; $i++) {
    echo "i value = " . i;
}

//while loop
$number = 1;
while ($number < 10) {
    echo 'value : ' . $number ;
    $number += 1;
}

//do while
$number = 1;
do {
    echo 'value : ' . $number ;
    $number += 1;
} while ($number < 10);

// foreach with break / continue exemple
$values = ['one', 'two', 'three'];
foreach ($values as $value) {
    if ($value === 'two') {
        break; // exit loop
    } elseif ($value === 'three') {
        continue; // next loop iteration
    }
}


Arrays


//Array declaration
$names = ['Mike', 'Peter', 'Shawn', 'John'];

//add to array
$names[] = 'Micheal';

// Array merge
$array3 = array_merge($array1, $array2);

// Array Concat with Spread Operator
$names = ['Mike', 'Peter', 'Paul'];
$people = ['John', ...$names]; // ['John', 'Mike', 'Peter', 'Paul']

//Remove array entry:
unset($names['Peter']);

//Array to string
echo implode(', ', $names) //output Mike, Shawn, John, Micheal

// String to Array
echo explode(',', $text); // ['Mike', 'Shawn', 'John']

// Direct access
echo $name[1] //output Peter

//loop for each array entry
foreach($names as $name) { 
   echo 'Hello ' . $name;
}

// Number of items in a Array
echo count($names);  

//Associative array:
$person = ['age' => 45, 'genre' => 'men'];

//Add to ass. array:
$person['name'] = 'Mike';

//loop ass. array key => value: 
foreach($names as $key => $value) { 
   echo $key . ' : ' . $value
}

// Check if a specific key exist
echo array_key_exist('age', $person);

// Return keys
echo array_keys($person); // ['age', 'genre']

// Return values
echo array_values($person) // [45, 'men']

//Array filter (return a filtered array)
$filteredPeople = array_filter($people, function ($person) {
    return $names->active;
})

// Array map (return transform array):
$onlyNames = array_map(function($person) {
    return [name => $person->name];
}, $people)


Functions


//function declararion
function name($firstName, $lastName = 'defaultvalue') {
    puts $firstName . ' ' . $lastName
}

//function call
name('Mike', 'Taylor');

//function call with named parameters (PHP 8)
name(firstName: 'Mike', lastName: 'Taylor'); // order can change

//function variables params
function name(...$params) {
    return $params[0] .   . params[1];
}

// Closure function
Route::get('/', function () {
     return view('welcome');
});

// Arrow functions
Route::get('/', fn () => view('welcome');


Files


//File read
$file = fopen("test.txt", "r");

//Output lines until EOF is reached
while(! feof($file)) {
  $line = fgets($file);
  echo $line. "<br>";
}
fclose($file);

// File write
$file = fopen('export.csv', 'a');
$array = ['name' => 'Mike', 'age' => 45];

//Write key name as csv header
fputcsv($file, array_keys($array[0]));

//Write lines (format as csv)
foreach ($array as $row) {
    fputcsv($file, $row); 
}
fclose($file);


Errors


//Throw Error
if (someCondition) {
    throw new Exception('Data format error');
}

//Catch the Error
try {
  $db->checkData($data)
} catch (Exception as $e)
    echo $e->getMessage();
}


OOP


//class declaration
class Person 
{
}

// object instantiation
$person = new Person

//class properties and constructor
class Person 
{
   protected $firstName;
   protected $lastName;
   public function __construct($firstName, $lastName) {
        $this->firstName = $firstName;
        $this->lastName = $lastName
   }

// Constructor Property Promotion (PHP 8)
class Person 
{
    public function __construct(protected $firstName, protected $lastName) 
    {

    }

//static constructor
public static function create(...$params) {
    return new self($params)
}
$person = Person::create(Mike, Taylor);

// class inheritance
class Customer extends Person
{
    public function name()
    {
        parent::name();
        echo 'Override method';  
    }
}

// Static Method
class greeting {
  public static function welcome() {
    echo "Hello World!";
  }
}

// Call static method
greeting::welcome();

// Static method internal call
class greeting {
  public static function welcome() {
    echo "Hello World!";
  }

  public function __construct() {
    self::welcome();
  }
}
new greeting();

// Interface
interface Animal {
  public function makeSound();
}

class Cat implements Animal {
  public function makeSound() {
    echo "Meow";
  }
}
$animal = new Cat();
$animal->makeSound();

//Trait (mix-in)
trait HelloWorld {
    public function sayHello() {
        echo 'Hello World!';
    }
}

class Greetings {
    use HelloWorld;
}
$object = new Greetings();
$object->sayHello();


https://dev.to/rickavmaniac/my-beloved-php-cheat-sheet-7dl