GithubHelp home page GithubHelp logo

php-syntax-cheat's Introduction

PHP Syntax Cheat beginner

Usefull natives functions

  • isset() test for variable exists
  • empty() test for empty variable
  • var_dump($var) DEBUG : displays the information of a variable
  • header() send a header from the server (redirect to)
  • is_numeric() test to see if a value is a number
  • exit() causes script top stop immediately
  • str_replace('a','b', $string) replace a with b in a string
  • trim($string) trims leading and trainling spaces
  • explode($separator, $string) makes array of strings, each of which is a substring of the string parameter extracted using the separator
  • implode($separator, $string) makes the elements of an array into a string
  • count($array) count all elements from an array, or something in an object
  • echo $string displays a string
  • print_r($array) displays readable information for a variable
  • require 'file.php' The include statement will issue E_ERROR if it cannot find the file, E_ERROR stops the script
  • include 'file.php' The include statement will issue E_WARNING if it cannot find the file, E_WARNING doesn't stop the script

Control structure

| if syntax

if (/*condition(s) here */) {
    // code here
}

=> or

if (/*condition(s) here */) {
    // code here
} else {
    // other code here
}

=> or

if (/*condition(s) here */) {
    // code here
} elseif (/*condition(s) here */) {
    // other code here
} else {
    // other code here
}

| Switch syntax

Can be used with numbers, or strings

switch ($age) {
    case $age < 15:
        echo $age;
        break;
    case $age >= 15 && $age <= 25:
        echo $age;
        break;
    case $age >= 26 && $age <= 30:
        echo $age;
        break;
    default:
        echo "Well done";
}

Functions

| Simple function

function functionName()
{
    // here code 
    return $result;
}

| Function with parameter(s)

function functionName($param1, $param2)
{
    // here code uses param1 & param2
    return $result;
}
  • Call function : functionName();
  • Call & print result of a function : echo functionName();

Loops

| FOR (loop until a condition is met)

for($i = 0; $i < 5; $i++){
    echo $i;
}
output =>
0
1
2
3
4

FOREACH (loop through an array)

$array = ['Paris','c\'est','Gotham'];
foreach ($array as $value) { 
    echo $value;
}
output =>
Paris
c'est
Gotham

WHILE (loop through query results)

$i = 0;
do {
    echo $i++;
} while ($i < 5)
output =>
0
1
2
3
4

Arrays

  • Create array : $myArray = array(); or $myArray = [];
  • Push into array : $myArray[] = "​Som​eth​ing​";
  • Push to associ​ative array : $myArr​ay[​'key'] = "​Val​ue";
  • Create numeric array : $myArray = ['va​lue', 'value2'];
  • Create associative array : $myArray = ['ke​y1'=​>'v​al1', 'ke​y2'=​>'v​al2'];
  • Print from numeric array : echo $myArray[0];
  • Print from associative array : echo $myArray['key'];
  • Print array : print_r($myArray);
  • Tips :
    • For associative array, keys are strings (example : 'firstname', 'lastname', 'email',...)
    • For numeric array, keys are numbers (Always 0, 1, 2...)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.