Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
In JavaScript the statement evaluates to
true
but in PHP it evaluates to
false
if ("0") {
    
}
by Valeri Tandilashvili
4 years ago
0
JavaScript
PHP
If / else
2
Log fatal error with details
If a fatal error occurs catch block calls
logError
function and passes details of the error, such as:
file
,
line
,
code
,
message
and
trace
try {
    undefinedFunction();
} catch (Error $e) {
    logError(
		'ERR FILE: ' . $e->getFile() . "\n" .
		'ERR LINE: ' . $e->getLine() . "\n" .
		'ERR CODE: ' . $e->getCode() . "\n" .
		'ERR TEXT: ' . $e->getMessage() . "\n" .
		'ERR TRACE String: ' . $e->getTraceAsString()
	);
}
The function
logError
appends current date and time to the error details and logs into the error file
function logError($error) {
	
	$content = "\n\n\n".date('Y-m-d H:i:s');
	$content .= "\n".$error;
	
	file_put_contents('/var/log/errors.txt', $content, FILE_APPEND);
}
by Valeri Tandilashvili
4 years ago
0
PHP
Try / Catch
2
splice CODE
Function
splice
changes content of an array. Let's first create an array of integers
let myFavoriteNumbers = [5, 14, 16, 18, 23, 25, 27, 50, 70]
If we want to remove one element from an array, we need to pass
1
as the second argument and the index of the item as the first argument. In this example
14
(second item) will be removed by passing
1 and 1
as arguments
myFavoriteNumbers.splice(1, 1);  // [5, 16,18, 23, 25, 27, 50, 70]
To remove two adjacent items, we need to pass first item's index
2
and quantity of items
2
that we want to remove. In this example we want to remove
18
and
23
from the array at the same time
myFavoriteNumbers.splice(2, 2);  // [5, 16, 25, 27, 50, 70]
Replaces 3 items starting from index 2 with
27
,
29
and
31
myFavoriteNumbers.splice(2, 2, 27, 29, 31);  // [5, 16, 27, 29, 31, 50, 70]
Removes all the items of the array except the first and the last items
myFavoriteNumbers.splice(1, 5);  // [5,70]
Adds the two new items
100
and
200
to the array after the first item
myFavoriteNumbers.splice(1, 0, 100, 200);  // [5, 100, 200, 70]
by Valeri Tandilashvili
4 years ago
0
JavaScript
Array functions
1
Handle fatal errors CODE
PHP 7.0 + can handle fatal errors without terminating PHP process. It means we can catch the fatal error using
catch
block
try {
    undefinedFunction();
} catch (Error $e) {
    echo 'Fatal error occurred: ' . $e->getMessage();
}
After running the above code on PHP 7+ the result will be:
Fatal error occurred: Call to undefined function undefinedFunction()
If we run the same code on PHP 5.6, we will get the following result:
<br />
<b>Fatal error</b>:  Call to undefined function undefinedFunction() in <b>[...][...]</b> on line <b>5</b><br />
by Valeri Tandilashvili
4 years ago
0
PHP
Try / Catch
1
Different types of data have access to different abilities or methods CODE
Let's declare any string variable
let myString = 'Some String'
The string variable has access to all
string functions
document.write(myString.toUpperCase());
The result will be:
SOME STRING
But if we let number variable to use the string function
toUpperCase()
let myNumber = 55
console.log(myNumber.toUpperCase());
Then we will get the following error. This is because number type variable does not have access to string functions
Uncaught TypeError: myNumber.toUpperCase is not a function
by Valeri Tandilashvili
4 years ago
0
JavaScript
String functions
2
Add new item to an array CODE
Creates
myFavoriteNumbers
array
let myFavoriteNumbers = [5, 14, 16, 18]
Adds new item
22
to the array
myFavoriteNumbers.push(22)
Adds the array as a new item to the
myFavoriteNumbers
array
myFavoriteNumbers.push([23, 24])
Adds the object to the array as new item
myFavoriteNumbers.push({firstNumber: 25, secondNumber: 26})
Adds the function to the array as new item
myFavoriteNumbers.push(function(){console.log('I am fired!')})
Adds new string item to the array
myFavoriteNumbers.push('27')
Let's fire the function that we added as an item
myFavoriteNumbers[7]()
After running the function the result will be:
I am fired!
by Valeri Tandilashvili
4 years ago
0
JavaScript
Arrays
1
Different types of arrays CODE
Creates array of numbers;
let myFavoriteNumbers = [5, 14, 16, 27]
Creates array of strings
let myFavoriteColors = ['green', 'yellow', 'red']
Creates array of arrays
let myMatrix = [
    [12, 23, 34], 
    [13, 35, 57], 
    [14, 47, 70], 
]
Creates array of objects
let myFavoritePlanets = [
    {name: 'Earth', radius: 6371}, 
    {name: 'Jupiter', radius: 69911}, 
    {name: 'Neptune', radius: 24622, moons: ['Triton', 'Thalassa', 'Nereid', 'Proteus', 'Hippocamp', 'Naiad']}
]
Accessing one of the moons of Neptune
console.log(myFavoritePlanets[2].moons[4])
by Valeri Tandilashvili
4 years ago
0
JavaScript
Arrays
1
The
json
method creates a
JSON
equivalent column. We can use
 $table->json('options');
to store json data in database
by Luka Tatarishvili
4 years ago
0
Laravel
database
migrations
laravel official doc
0
Secure password rules
We need to follow the rules if we want our passwords to be strong enough
1. Contains at least 8 characters
2. Contains at least one uppercase letter
3. Contains at least one lowercase letter
4. Contains at least one number
5. Contains at least one special character
by Valeri Tandilashvili
4 years ago
0
PHP
Security
1
Json stringify / parse
While sending ajax we can convert object into a string with
JSON.stringify()
function...
$.ajax({
            url: '/api/page/delete',
            type: 'DELETE',
            data: JSON.stringify({
                'id': id,                   
                 '_token': ' {{ csrf_token() }} '
             }),
Then parse the result to reach data item:

success: function(result) {
                    parsed_result = JSON.parse(result)
                    items = parsed_result.data.items
}
by Luka Tatarishvili
4 years ago
0
JavaScript
json
0
Results: 1578