×
Clear all filters including search bar
Valeri Tandilashvili's Personal Professional Blog
empty
and isset
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
The following values are considered to be false:
""
(an empty string)
0
(0 as an integer)
0.0
(0 as a float)
"0"
(0 as a string)
NULL
FALSE
array()
(an empty array).btn-block
<button type="button" class="btn btn-primary btn-lg btn-block">Block level button</button>
<button type="button" class="btn btn-secondary btn-lg btn-block">Block level button</button>
composer remove vendor/package vendor/package2
// Example usage:
$year = 2023;
$month = 1; // May
function generateMonthArray($year, $month) {
$numDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$firstDay = date("N", strtotime("$year-$month-01")); // 1 = Monday, 7 = Sunday
$weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
$monthArray = array_fill_keys($weekdays, []);
for ($day = 1; $day <= $numDays; $day++) {
$weekday = ($firstDay + $day - 2) % 7; // Adjust to start from Monday
$monthArray[$weekdays[$weekday]][] = $day;
}
foreach ($monthArray as &$weekDays) {
if ($weekDays[0]!=1) {
array_unshift($weekDays, '');
} else {
break;
}
}
return $monthArray;
}
$result = generateMonthArray($year, $month);
// Print the result
foreach ($result as $weekday => $days) {
echo $weekday . "\t" . implode("\t", $days) . "\n";
}
scripts
key, inside package.json
file.
If the content is the following"scripts": {
"start": "node app.js",
"server": "live-server"
}
Then we can run npm run start
and npm will run node app.js
We can also run npm run server
and it will run live-server
printMe()
is defined after it's called but it works without errorsecho printMe();
function printMe() {
return 'Print some text';
}
Exception is conditional function
.
In this example function foo()
will not be defined until the if ($makefoo)
conditional statement gets executed$makefoo = true;
/* We can't call foo() from here
since it doesn't exist yet,
but we can call bar() */
// foo();
bar();
if ($makefoo) {
function foo()
{
echo "I don't exist until program execution reaches me.\n";
}
}
/* Now we can safely call foo()
since $makefoo evaluated to true */
if ($makefoo) foo();
function bar()
{
echo "I exist immediately upon program start.\n";
}
Functions within functions.
Function bar()
will not be defined until the function foo()
is executedfunction foo()
{
function bar()
{
echo "I don't exist until foo() is called.\n";
}
}
/* We can't call bar() yet
since it doesn't exist. */
// bar();
foo();
/* Now we can call bar(),
foo()'s processing has
made it accessible. */
bar();
Note: All functions in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versaclass Foo {
public function __call($function, $arguments)
{
return "$function has been called with arguments: " . implode(', ', $arguments);
}
// public function fireFunction($one, $two, $three) {
// return "pre-defined method";
// }
}
$foo = new Foo();
echo $foo->fireFunction(5, 47, "third argument");
__call
is invoked when the method is inaccessible (when it's not public or does not exist at all)__get
and __set
magic methods allows us to get and set any variable without having individual getter and setter methods:class Foo {
protected $properties = array();
public function __get( $key )
{
if(array_key_exists($key, $this->properties)){
return $this->properties[$key];
}
return $this->properties[ $key ];
}
public function __set( $key, $value )
{
$this->properties[ $key ] = $value;
}
}
$foo = new Foo();
$foo->name = 'David';
echo $foo->name;
In this example, we can set any variable, even if it does not exist. Every variable will be addd to properties
arrayvariableVAR
is visible outside the if
statement because it uses function scope instead of block scopefunction callMePlease() {
if (1 == 1) {
let variableLET = 'LET'
var variableVAR = 'VAR'
}
document.write('My ' + variableVAR + '</br>')
document.write('My ' + variableLET + '</br>')
}
callMePlease()
Another variable variableLET
is not visible outside the block because it uses block scope and the code above will generate the following error:Uncaught ReferenceError: variableLET is not defined
href
, text
of a link
JS//calling the function in window.onload to make sure the HTML is loaded
window.onload = function() {
var el = document.getElementsByTagName('a');
el[0].href= 'http://www.sololearn.com';
};
HTML<a href="http://www.example.com">Some link</a>
<br />
<a href="https://google.com">Google.com</a>