$variable = 1;
JS alternative
let variable = 1;
PHP - check if the variable exists
if (isset($variable))
JS alternative
if (typeof variable !== "undefined")
PHP count array elements
$count = count([2, 3, 4]);
JS alternative
let count = [2, 3, 4].length;
PHP count letters in the string
$len = strlen('some string');
JS alternative
let len = 'some string'.length;
PHP absolute value of a given number
$variable = abs(-5);
JS alternative
let variable = Math.abs(-5);
PHP - power of a number
$square = pow(5, 2);
JS alternative
let square = Math.pow(5, 2);
PHP - square root of a number
$square_root = sqrt(16);
JS alternative
let square_root = Math.sqrt(16);
PHP - minimum number
$max = min(2, 3, 4);
JS alternative
let max = Math.min(2, 3, 4);
PHP - maximum number
$max = max([2, 3, 4]);
JS alternative
let max Math.max(...[2, 3, 4])
PHP calculates sum of the array elements
$sum = array_sum([2, 3, 4]);
JS alternative
let sum = 0;
let arr = [1, 2, 3];
for (let value of arr) {
sum += value;
}
Another way to calculate the same
let arr = [1, 2, 3];
let sum = 0;
let len = arr.length;
for (let i = 0; i < len; sum += arr[i++]);
PHP - string position
$pos = strpos('Hello world', 'world');
JS alternative
let pos = 'Hello world'.indexOf('world');
PHP - edit letter in a string
$string[4] = 'X';
JS alternative
string = string.substring(0, 4) + 'X' + string.substring(5);
PHP convert the variable to integer
$variable = intval(5.5);
JS alternative
let variable = parseInt(5.5);
PHP - explode the sentence
$sentence = "Lorem ipsum dolor st amet";
$words = explode(" ", $sentence);
JS alternative
let sentence = "Lorem ipsum dolor st amet";
let words = sentence.split(" ");
git init
Step 2: create index.html file and 2 commit m1,m2
add text "m1" into index.html and commit as m1
echo "m1" > index.html
git add .
git commit -m "m1"
add text "m2" into index.html and commit as m2
echo "m2" >> index.html
git add .
git commit -m "m2"
Step 3: create feature branch and checkout
git checkout -b feature
Step 4: create feature folder&file
create feature.html into the feature folder and add text "f1" then commit as f1
echo "f1" >> feature/feature.html
git add .
git commit -m "f1"
Step 5: Checkout to master
git checkout master
Step 6: add text "m3" into index.html and commit as m3
echo "m3" >> index.html
git add .
git commit -m "m3"
Step 7: Checkout to feature
git checkout feature
Step 8: add text "f2" into feature.html and commit as f2
echo "f1" >> feature/feature.html
git add .
git commit -m "f2"
Step 9: Checkout to master
git checkout master
Step 10: Merge feature branch into master with squash
git merge --squash feature
Step 11: See git status because files we will have into the staging area and we need create a commit for it
git status
git commit -m "Merge feature branch into master"
source
code4123
new code line
one more line1hbefore
insert
and checks & replaces gender valuesDROP TRIGGER IF EXISTS before_student_insert;
DELIMITER $$
CREATE TRIGGER before_student_insert BEFORE INSERT
ON students
FOR EACH ROW
BEGIN
INSERT INTO log (description)
VALUES ('One student will be inserted');
IF (NEW.gender = 'Female') THEN
SET NEW.gender = 2;
ELSE
SET NEW.gender = 1;
END IF;
END$$
DELIMITER ;
CASCADE
- deletes or updates appropriate rows in the child table
SET NULL
- sets the foreign key column of the child table to NULL
RESTRICT
- rejects the operation for the parent table
NO ACTION
- equivalent to RESTRICT
Note: RESTRICT
is the same as omitting the ON DELETE
or ON UPDATE
clause, which means that it's default actionDROP FUNCTION IF EXISTS getIncomeLevel;
DELIMITER //
CREATE FUNCTION getIncomeLevel ( monthly_value INT )
RETURNS varchar(20)
BEGIN
DECLARE income_level varchar(20);
IF monthly_value <= 4000 THEN
SET income_level = 'Low Income';
ELSEIF monthly_value > 4000 AND monthly_value <= 7000 THEN
SET income_level = 'Avg Income';
ELSE
SET income_level = 'High Income';
END IF;
INSERT INTO log (description)
VALUES ('getIncomeLevel function was called');
RETURN income_level;
END; //
DELIMITER ;
After creating the function we can call it:SELECT getIncomeLevel(450)
DROP PROCEDURE IF EXISTS printIncomeLevel;
DELIMITER //
CREATE PROCEDURE printIncomeLevel (IN monthly_value INT)
BEGIN
DECLARE income_level varchar(20);
IF monthly_value <= 4000 THEN
SET income_level = 'Low Income';
ELSEIF monthly_value > 4000 AND monthly_value <= 7000 THEN
SET income_level = 'Avg Income';
ELSE
SET income_level = 'High Income';
END IF;
INSERT INTO log (description)
VALUES ('getIncomeLevel procedure was called');
SELECT income_level;
END;
After creating the procedure, we can call it using CALL
keyword:CALL printIncomeLevel(450)
INDEX
with several fields for table notes
.
Combines the two fields title, description
to create one indexCREATE TABLE note (
id int AUTO_INCREMENT,
note_id int,
title varchar(50),
description varchar(50),
PRIMARY KEY(id, note_id),
INDEX ind_title_description (title, description)
);
If we need to filter the result with both fields (title, description)
at the same time, then ind_title_description
is a great indexnote
and adds several indexes (ind_note_id, ind_title_description)
at the same timeCREATE TABLE note (
id int AUTO_INCREMENT,
note_id int,
title varchar(50),
description varchar(50),
PRIMARY KEY(id, note_id),
INDEX ind_note_id (note_id),
INDEX ind_title_description (title, description)
);