Install Drush - a command-line shell and Unix scripting interface for Drupal.
Place file script.php under the Drupal root directory:
<?php
use Drupal\Core\Update\UpdateKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
// Disable garbage collection during test runs. Under certain circumstances the
// update path will create so many objects that garbage collection causes
// segmentation faults.
require_once 'core/includes/bootstrap.inc';
$database = \Drupal::database();
// Do something.
To execute a standalone php script, run drush scr script.php.
For example if Drupal root directory is web/, place file web/scripts/script.php:
<?php
use Drupal\Core\Update\UpdateKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once dirname(__DIR__) . '/autoload.php';
// Disable garbage collection during test runs. Under certain circumstances the
// update path will create so many objects that garbage collection causes
// segmentation faults.
require_once dirname(__DIR__) . '/core/includes/bootstrap.inc';
$database = \Drupal::database();
// Do something.
To execute a standalone php script from the subfolder, run drush scr web/scripts/script.php.