Results: 1022
return false if any member is greater than 4
The following function will return false if any number in an array of numbers is greater than 4. Otherwise, the function will return true
function lessThanFive(numbers) {
  for (let number of numbers) {
    if (number > 4) {
      return false;
    }
  }
  return true;
}
Multiple return in a function
It can be useful to have multiple returns in a function declaration as long as only 1 return is expected to run. For example:
function invert(x) {
  if (x === 0) {
     return 'input was zero';
  } else {
    return 1 / x;
  }
}
The function will still work using an if statement instead of an if...else statement
function invert(x) {
  if (x === 0) {
     return 'input was zero';
  }
  return 1 / x;
}
Content of the logger module
const EventEmitter = require('events');

class Logger extends EventEmitter {
  log(message) {
    
    // Log the message
    console.log(message);
    
    // Raise an event
    this.emit('messageLogged', { id: 1, url: 'http://' });
  }
}

module.exports = Logger;
Content of the index file
const Logger = require('./logger');
const logger = new Logger();

// Register a listener
logger.on('messageLogged', function(arg) {
  console.log('Listener called', arg);
});

logger.log('message');
Event emitter raises the event called
messageLogged
and passes the object as an argument
const EventEmitter = require('events');
const emitter = new EventEmitter();

// Register a listener
emitter.on('messageLogged', function(obj) {
  console.log('Listener called', obj);
});

// Raise an event & passes the object
emitter.emit('messageLogged', {key1: 'Value 1', key2: 'Value 2'});
Event emitter raises the event called
messageLogged
and passes parameters
const EventEmitter = require('events');
const emitter = new EventEmitter();

// Register a listener
emitter.on('messageLogged', function(arg1, arg2) {
  console.log('Listener called', arg2, arg1);
});

// Raise an event
emitter.emit('messageLogged', 'arg-first', 'arg-second');
Event emitter raises the event called
messageLogged
const EventEmitter = require('events');
const emitter = new EventEmitter();

// Register a listener
emitter.on('messageLogged', function() {
  console.log('Listener called');
});

// Raise an event
emitter.emit('messageLogged');
parse_ini_file
The function
parse_ini_file()
returns the settings as an associative array from
ini
file
session_write_close
Sessions automatically end when the PHP script finishes executing but can be manually ended using the
session_write_close()
function
ctype_alnum
function
ctype_alnum
returns true if the value contains only alphanumeric letters. In the example
IF
statement will be executed because the value is alphanumeric
$var = 'as2df234asdf';

if (ctype_alnum($var)) {
	echo 'if';
} else {
	echo 'else';
}
Reads the current directory synchronously
const fs = require('fs')

// With Sync function
let files = fs.readdirSync('./')
console.log(files)
Reads the current directory asynchronously
const fs = require('fs')

// With Async function
fs.readdir('./', function(err, files){
    if (err) {
        console.log('Error', err)
    } else {
        console.log(files)
    }
})
Results: 1022