PHP basic grammar

Aug 27, 2020 PHP

Introduction

I’m a beginner who has just learned PHP. If there are any mistakes, we would appreciate it if you could point them out.

##print syntax

<?php
print ("I'm studying PHP!!");
?>

If you want to display double quotes, use escape sequence

<?php
print ("I'm studying PHP!\"kirin\"");
?>

Summary of simple escape sequences

Escape Sequence Effect
\n Line feed
\r Carriage return
\t Tab
\ \
$ $
" "
' '

What is a carriage return? Control character that returns the cursor to the beginning

Arithmetic operator

If you enclose it in “”, it will be evaluated as a character string. Note*

<?php
print (1+1);#2
?>

Complex calculation is possible by combining

<?php
print ((100**3+12)*3);#3000036
?>

Summary of simple operators

Symbol Effect
+ Addition
- Subtraction
* Multiplication
** n-th power
/ Division
% Remainder calculation

##date

date (string $format [, int $timestamp = time() ]) :string You can have two arguments

The time changes each time the screen is displayed Date and time can be obtained using date function, but not displayed

https://www.php.net/manual/ja/function.date.php

Date and day display

<?php
print (date('F/j/Y'));
#Displayed results: August/27/2020
?>

Designated characters of ###date

Symbol Effect Example of use
Y (4 digit display) Year 2020
y (2-digit display) Year 20
m (2-digit display) Month 08
M (English display) Month Aug
F (English) Month August
n (clear 0) Month 8
d Sun 15
H (24-hour display) Hours 15
h (12 hour display) Time 3
i min 59
s seconds 59
t Days of Month 31
D (3 letters) Day of the week Sat
w Quantify day of week (0-6) 6

Time display is also possible Time zone setting is required to display Japan time

<?php
print (date('G hour i minutes s seconds'));
?>

Set the time zone as follows

<?php
date_default_timezone_set('Asia/Tokyo');
print (date('G hour i minutes s seconds'));
?>

concatenate strings

You can connect strings that require "” by using.

<?php
date_default_timezone_set('Asia/Tokyo');
print ('current time:'.date('G hour i minute s second'));
?>

Object-oriented

format is called a method and formats the acquired time DateTime() is an object with various date and time methods

PHP has a mixture of procedural and object-oriented types

<?php
date_default_timezone_set('Asia/Tokyo');
$today = new DateTime();
print ($today ->format('G hour i minutes s seconds'));
#Display result: 8:12:09
?>

###time stamp Returns the time elapsed since January 1, 1970

<?php
    print (time())
    ?>

###strtotime strings to time Converts the specified string as a time stamp

<?php
    print (date('n/j(D)')."\n");
    // Display result: 8/27(Thu)
    print (date('n/j(D)',strtotime("+2day")));
    // Display result: 8/29(Sat)
    ?>

##variable

PHP variable features

<?php $sum = 100 + 200 + 300; ?>
    Total amount is: <?php print ($sum); ?> Yen
#Display result: Total amount is 600 yen
    Tax included: <?php print ($sum*1.1); ?> Yen
#Display result: 660 yen including tax

##while syntax

<?php
    $i =1;
    while ($i <= 10){
        print ($i."\n");
        $i += 1;
    }
    ?>

Call using endwhile

<?php
    $i =1;
    while ($i <= 10):
        print ($i."\n");
        $i += 1;
    endwhile;
    ?>

Comparison operator

Symbol Effect
A <B A is less than B
A <= B A is less than or equal to B
A === B A and B are equal
A !== B A and B are not equal
!A Not A

increment decrement

For division and multiplication, there is no such thing as an increment because the value does not change even if it is multiplied by 1.

Symbol Effect 1 Effect 2
$i = $i + 1 $i += 1 $i ++
$i = $i-1 $i -= 1 $i -

##for syntax for(Initialization process; Repeat condition; Update process;){ The process you want to repeat; }

<?php
    for ($i=1; $i<=10; $i++){
        print ($i."\n");
    }
    ?>

Call by using endfor instead of {}

<?php
    for ($i=1; $i<=10; $i++):
        print ($i."\n");
    endfor;
    ?>

Output day and day of the week up to

<?php
    for($i=0; $i<=365; $i++){
        print (date('Y/n/j(D)',strtotime("+".$i."day")));
        print "\n";
    }
    ?>

array

array() was used before PHP 4.5 You need to specify the index to display the array

When you want to output the day of the week in Japanese

<?php
    $week_name = ["Day", "Month", "Tue", "Wed", "Thurs", "Friday", "Day"];
    print($week_name[date('w')]);
    ?>

###Associative array By specifying key, values can be retrieved from the list regardless of the order of the array

<?php
    $drink = ['tea' =>'tea',
        "Cola" =>'coke',
        "Coffee" =>'coffee',
        "Wine" =>'wine'];
    print ($drink["tea"]);
#tea
    ?>

###foreach syntax Array-only syntax Each element can be output repeatedly

<?php
    $drink = ['tea' =>'tea',
        "Cola" =>'coke',
        "Coffee" =>'coffee',
        "Wine" =>'wine'];

    foreach ($drink as $val){
        print ($val."\n");
    }
    ?>

Output result

tea
coke
coffee
wine

If you also want to retrieve the index

<?php
    $drink = ['tea' =>'tea',
        "Cola" =>'coke',
        "Coffee" =>'coffee',
        "Wine" =>'wine'];
    foreach ($drink as $japanese => $english){print ($japanese." :".$english."\n");
    }
    ?>

Output result

Tea: tea
Coke: coke
Coffee: coffee
Wine: wine

##if syntax

<?php
    date_default_timezone_set('Asia/Tokyo');
    if(12 <= date("G")){
        print ("You can eat lunch");
    }
    else{
        print ("Dinner time");
        }
    ?>

False if the string is empty, True otherwise False if the number is 0, True otherwise

Round up, round down, round

round off at floor

<?php
    print (floor(10/4))
// 2
    ?>

round up with ceil

<?php
    print (ceil(10/4))
// 3
    ?>

round to round How many decimal places the second argument has

<?php
    print (round(1/3,3))
// 0.333
    ?>

##sprintf It is possible to arrange the number of digits

d indicates a numerical value , so 0 is output when a character string is entered.

<?php
    $date = sprintf("%04d year %02d month %02d day", 2020,8,27);
    print ($date);
// August 27th, 2020
    ?>

String uses s

<?php
    $date = sprintf("%04d year %02d month %02d day %s", 2020,8,27,"Thurs");
    print ($date);
// Thursday, August 27th, 2020
    ?>