<commit_before>---
layout: post
title: My PHP Notes
date:   2016-10-11 18:24:49
categories: front-end notes
---

> Learnd from [Codecademy](https://www.codecademy.com/)

## Several functions

1. strlen
    `$length = strlen($example);`
2. substr
    `$subString = substr($example, 0, 4); // 0 is starting position; 4 is length`
3. strtoupper
    `$uppercase = strtoupper($example);`
4. strtolower
    the same with 3
5. strpos
    `$position = strpos($example, "q"); `
6. M_PI
    a PHP constant that is equal to _pi_
7. round
    `$integerNum = round($floatNum);`
    `$twoDecimalPlaces = round($floatNum, 2);`
8. rand
    `print rand(); // prints a number between 0 and 32767`
    `print rand(1, 10); // prints a number between 1 and 10`
9. array
    `$anArray = array();`
10. array_push
    `array_push($anArray, "item");`
11. count
    `count($anArray); // return the number of elements of the array`
12. sort
    `sort($anArray); // sequense from small to big`
13. rsort
    `rsort($anArray); // sequense from big to small`
14. join
    `join(",", $anArray); // make an array into a string, splited by ","`

## Object Oriented PHP
``` PHP
<!DOCTYPE html>
<html>
    <head>
      <title> Introduction to Object-Oriented Programming </title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
    </head>
	<body>
      <p>
      <?php
        // The code below creates the class
        class Person {
            // Creating some properties (variables tied to an object)
            public $isAlive = true;
            public $firstname;
            public $lastname;
            public $age;

            // Assigning the values
            public function __construct($firstname, $lastname, $age) {
              $this->firstname = $firstname;
              $this->lastname = $lastname;
              $this->age = $age;
            }

            // Creating a method (function tied to an object)
            public function greet() {
              return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
            }
          }

        // Creating a new person called "<NAME>", who is 12345 years old ;-)
        $me = new Person('boring', '12345', 12345);

        // Printing out, what the greet method returns
        echo $me->greet();
        ?>
        </p>
    </body>
</html>
```
1. Create a new class
    `class Person {}`
2. Construct an instance of the class
    `$instance = new Person();`
3. print a public property from a class
    ``` PHP
    class Person {
      public $theValue = true;
    }

    $example = new Person();

    print $example->theValue; // true
    ```
4. methods: functions bundled into objects
    ``` PHP
    class Person {
      public function __construct($para1, $para2) {
        this->num1 = $para1;
        this->num2 = $para2;
      }
    }
    $obj = new Person("one", "two");
    print $obj->num1; // one
    ```<commit_msg>modify: php note syntax<commit_after>---
layout: post
title: My PHP Notes
date:   2016-10-11 18:24:49
categories: front-end notes
---

> Learnd from [Codecademy](https://www.codecademy.com/)

## Several functions

1. strlen
    `$length = strlen($example);`
2. substr
    `$subString = substr($example, 0, 4); // 0 is starting position; 4 is length`
3. strtoupper
    `$uppercase = strtoupper($example);`
4. strtolower
    the same with 3
5. strpos
    `$position = strpos($example, "q"); `
6. M_PI
    a PHP constant that is equal to _pi_
7. round
    `$integerNum = round($floatNum);`
    `$twoDecimalPlaces = round($floatNum, 2);`
8. rand
    `print rand(); // prints a number between 0 and 32767`
    `print rand(1, 10); // prints a number between 1 and 10`
9. array
    `$anArray = array();`
10. array_push
    `array_push($anArray, "item");`
11. count
    `count($anArray); // return the number of elements of the array`
12. sort
    `sort($anArray); // sequense from small to big`
13. rsort
    `rsort($anArray); // sequense from big to small`
14. join
    `join(",", $anArray); // make an array into a string, splited by ","`

## Object Oriented PHP
``` PHP
<!DOCTYPE html>
<html>
    <head>
      <title> Introduction to Object-Oriented Programming </title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
    </head>
	<body>
      <p>
      <?php
        // The code below creates the class
        class Person {
            // Creating some properties (variables tied to an object)
            public $isAlive = true;
            public $firstname;
            public $lastname;
            public $age;

            // Assigning the values
            public function __construct($firstname, $lastname, $age) {
              $this->firstname = $firstname;
              $this->lastname = $lastname;
              $this->age = $age;
            }

            // Creating a method (function tied to an object)
            public function greet() {
              return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
            }
          }

        // Creating a new person called "boring 12345", who is 12345 years old ;-)
        $me = new Person('boring', '12345', 12345);

        // Printing out, what the greet method returns
        echo $me->greet();
        ?>
        </p>
    </body>
</html>
```
1. Create a new class
    `class Person {}`
2. Construct an instance of the class
    `$instance = new Person();`
3. print a public property from a class

``` PHP
class Person {
  public $theValue = true;
}

$example = new Person();

print $example->theValue; // true
```

4. methods: functions bundled into objects

``` PHP
class Person {
  public function __construct($para1, $para2) {
    this->num1 = $para1;
    this->num2 = $para2;
  }
}
$obj = new Person("one", "two");
print $obj->num1; // one
```
{'fraction_non_alphanumeric': 0.14197424892703864, 'fraction_numerical': 0.027296137339055795, 'mean_word_length': 2.2438752783964366, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 52, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7369981', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1791, 'n_words': 618}