×
Clear all filters including search bar
Valeri Tandilashvili's Personal Professional Blog
textarea
instead of using input
element function copy(part1, part2) {
// Creates textarea element
var textarea = document.createElement('textarea');
// Puts the text into the textarea with breaklines
textarea.value = part1 + "\n\n" + part2
// Adds the element to the DOM
document.body.appendChild(textarea);
// Selects the text that we want to copy
textarea.select();
// Copies the selected text clipboard
var result = document.execCommand('copy');
// Removes the element from the DOM because we no longer need it
document.body.removeChild(textarea);
return result;
}
class Say {
public function let() {
echo 'Let' . " ";
return $this;
}
public function me() {
echo 'Me' . " ";
return $this;
}
public function tell() {
echo 'Tell' . " ";
return $this;
}
public function you() {
echo 'You' . " ";
return $this;
}
public function something() {
echo 'Something' . " ";
}
}
$phrase = new Say;
$phrase->let()->me()->tell()->you()->something();
__get
and __set
methods are called when there is an attempt either to use or to set a new value to any of the class propertyclass Person{
public $firstName;
public function __get($propertyName){
echo "attempted to read property: $propertyName" . PHP_EOL;
}
public function __set($propertyNane, $propertyValue){
echo "attempted to set new value to property: $propertyNane" . PHP_EOL;
}
}
$p = new Person();
$p->firstName = 'Doe';
echo $p->firstName . PHP_EOL;
$p->lastName = 'John';
echo $p->lastName;
If the property is public
, the methods are not going to be called (if the property does not exist, or exists but is not public, the methods will get called)
If we make $firstName
property private
or protected
, the methods will be called for the $firstName
property
__get
, __set
, __call
and __callStatic
are invoked when the method or property is inaccessibleclass Wheather {
public $nonStatic = 0;
public static $tempConditions = ['cold', 'mild', 'warm'];
public static $someProperty = 5;
static function celsiusToFarenheit($celsius) {
return $celsius * 9 / 5 + 32 + $this->nonStatic;
}
}
echo Wheather::celsiusToFarenheit(0);
echo "\n";
In this case we will get the error:
Fatal error: Uncaught Error: Using $this when not in object context
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('dashboard')->with('posts', $user->posts);
git commit
-> i
- to jump into insert mode
-> esc
- takes us out of insert mode
-> :w
- to save written comment
-> :q
- to quit
------------------------
:w
and :p
can be combined into :wq
if
has only one section: Condition
if (Condition) {
// Code to be executed
}
for
loop has three sections divided by ;
for (Initialization; Condition; Increment/Decrement) {
// Code to be executed
}
The loop prints numbers from 0 to 10
for ($i = 0; $i < 10; $i ++) {
echo $i . "\n";
}
function printGreeting() {
print "Prints: Hello world!\n";
}
printGreeting();
Function that returns something
function returnGreeting() {
return "Returns: Hello world!\n";
}
echo returnGreeting();
Parameterized function that doubles the number
function doubleMe($number) {
$result = "$number times 2 is: " . $number * 2 . "\n";
return $result;
}
echo doubleMe(4);
Function calculates the sum of two numbers
function add($num1, $num2) {
$sum = $num1 + $num2;
$result = "Sum of $num1 and $num2 is: $sum";
return $result;
}
echo add(4, 5);
$d = date("D");
if($d == "Fri"){
echo "Have a nice weekend!";
} else{
echo "Have a nice day!";
}
echo "\n";
Output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
If $age
is more than 18
, prints Adult
otherwise prints Child
$age = 20;
if ($age > 18) {
echo 'Adult';
} else {
echo 'Child';
}
strlen()
returns the length of the string
echo strlen("Hello world!")."\n"; // 12
Function strrev()
returns the Reverse of the string
echo strrev("Hello world!")."\n"; // !dlrow olleH
Function strpos()
tries to find the second parameter and returns its index
echo strpos("Hello world!", "world")."\n"; // 6
Function str_replace()
replaces the word "Hello" with "Hi"
echo str_replace("Hello", "Hi", "Hello there")."\n"; // Hi there
Function substr()
returns the substring between given indexes
echo substr("Hello world", 6)."\n"; // world
echo substr("Hello world", 6, 3)."\n"; // wor
echo substr("Hello world", -5)."\n"; // world
echo substr("Hello world", -4, 2)."\n"; // or
Function trim()
removes whitespaces from sides of the string
echo trim(" Hello World ").".\n"; // Hello World.
// Removes whitespaces from the right end
echo rtrim(" Hello World ")."\n"; // Hello World
// Removes Exclamation mark from the right end
echo rtrim("Hello World!", "!")."\n"; // Hello World
Function strtoupper()
converts the string to upper case
echo strtoupper("Hello World")."\n";
Function strtolower()
converts the string to lower case
echo strtolower("Hello World")."\n";
Function explode()
breaks the string by its delimiter
print_r (explode(" ", "It's the first day of the course!"));
Function implode()
joins the array element based on the delimiter and returns the string
echo implode(" ", ['Hello', 'World']);