- https://laracasts.com/series/php-for-beginners
- start a server with
php -S localhost:8888
- commands operations end with
;
- variables start with
$
$var
- period
.
is used for concatenationecho 'Hello ,' . $var;
- double quotes for printing the variable in a string
echo "Hello , $var";
- curly braces can be used for better readability
echo "Hello, {$var}";
- a file with 100% php has no php closing tag
?>
$_GET['var']
gets the value of a key value pair in the url- with url
http://localhost:8888?var=foo
,$_GET['var']
will returnfoo
- with url
htmlspecialchars()
; Convert special characters to HTML entities- useful for making sure no html is parsed in
$_GET
, aka sanitize
- useful for making sure no html is parsed in
- use
<?=
as a shorthand for<?php echo
- end css statements with
;
- comment out html with
<!-- this is a comment -->
require <file.php>
loads an external php file- allows separation of logic and rendering
-
$myArray[]
defines an empty array -
assign values
$myArray=[ 'item1', 'item2' ]
-
loop over array
<?php foreach($myArray as $item){ // do stuff } ?>
- or
<?php foreach($myArray as $item) : ?> // do stuff <?php endforeach; ?>
- key value pairs use arrow
=>
$myArray['key'=>'value]
- add key/value to existing array
$myArray['newKey']='newValue'
- add value to non associative array
myArray[]='newValue'
- remove key/value
unset(myArray['key'])
- dump variable info with
var_dump($myVar)
die('message')
stops execution- e.g.
die(var_dump($myVar))
- e.g.
- boolean values
true
/false
- e.g.
$completed = true;
- e.g.
- ternary operator
$condition ? 'do this when true' : 'do this when false';
ucwords('uppercase the first character of each word in a string')
-
<?php if (condition) { // do stuff } else { // do stuff } ?>
-
or
<?php if(condition) : ?> // do html stuff <?php endif; ?>
-
with
else
<?php if(condition) : ?> //do html stuff <?php else : ?> // do other html stuff <?php endif; ?>
-
use
!
for not! true
==false
-
function functionName($arg){ // do stuff // optionally return $returnValue = true; return $returnValue; }
- using mariadb instead of mysql ;-)
- cd into step_11
docker compose up -d
docker compose exec db mariadb -uroot
- sql commands end with
;
- some commands
-
show databases;
-
create database mytodos;
-
use mytodos;
-
create table todos (id integer PRIMARY KEY AUTO_INCREMENT, description text NOT NULL, completed boolean NOT NULL);
-
show tables; +------------------+ | Tables_in_mytodo | +------------------+ | todos | +------------------+ 1 row in set (0.016 sec)
-
describe todos; +-------------+------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | description | text | NO | | NULL | | | completed | tinyint(1) | NO | | NULL | | +-------------+------------+------+-----+---------+----------------+ 3 rows in set (0.097 sec)
-
drop table table_name;
-
insert new record
-
insert into todos (description, completed ) values('Go to the store', false);
-
-
show records
-
select * from todos; +----+-----------------+-----------+ | id | description | completed | +----+-----------------+-----------+ | 1 | Go to the store | 0 | +----+-----------------+-----------+ 1 row in set (0.001 sec)
-
-
- define a class with
class
- properties
protected $prop
- can also be 'private' and 'public'
- constructor
public function __construct($prop)
- access member functions and properties with
$this->prop
- note that
prop
gets no$
- note that
- create new object with
new
new myClass('property');
- constructors can take arguments
-
connect to database with
- SQLite:
PDO("sqlite:path_to_db);
- MySQL:
PDO('mysql:host=172.21.0.2;dbname=mytodos', 'root', '');
- SQLite:
-
query in two parts
prepare('QUERY_STRING')
execute()
- split and move different functions over different files
static
function : use function without instantiating a class- NOTE: php files can return value (see
bootstrap.php
)
- move database configurations in
config.php
- restructure code in core / controllers / views
$_SERVER['REQUEST_URI']
returns URIstatic
functionRouter::load($var)
- create new
Router
object with$router = new static;
- can't return
$this
because it's a static object - use
return $router
- create new