true
but in PHP it evaluates to false
if ("0") {
}
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 filefunction 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);
}
splice
changes content of an array.
Let's first create an array of integerslet 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 argumentsmyFavoriteNumbers.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 timemyFavoriteNumbers.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 itemsmyFavoriteNumbers.splice(1, 5); // [5,70]
Adds the two new items 100
and 200
to the array after the first itemmyFavoriteNumbers.splice(1, 0, 100, 200); // [5, 100, 200, 70]
catch
blocktry {
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 />
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 functionsUncaught TypeError: myNumber.toUpperCase is not a function
myFavoriteNumbers
arraylet myFavoriteNumbers = [5, 14, 16, 18]
Adds new item 22
to the arraymyFavoriteNumbers.push(22)
Adds the array as a new item to the myFavoriteNumbers
arraymyFavoriteNumbers.push([23, 24])
Adds the object to the array as new itemmyFavoriteNumbers.push({firstNumber: 25, secondNumber: 26})
Adds the function to the array as new itemmyFavoriteNumbers.push(function(){console.log('I am fired!')})
Adds new string item to the arraymyFavoriteNumbers.push('27')
Let's fire the function that we added as an itemmyFavoriteNumbers[7]()
After running the function the result will be: I am fired!
let myFavoriteNumbers = [5, 14, 16, 27]
Creates array of stringslet myFavoriteColors = ['green', 'yellow', 'red']
Creates array of arrayslet myMatrix = [
[12, 23, 34],
[13, 35, 57],
[14, 47, 70],
]
Creates array of objectslet 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 Neptuneconsole.log(myFavoritePlanets[2].moons[4])
json
method creates a JSON
equivalent column.
We can use $table->json('options');
to store json data in database1. 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
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
}