Skip to content

Instantly share code, notes, and snippets.

@itsdonnix
Created November 21, 2024 09:27
Show Gist options
  • Save itsdonnix/301b3f554a9f97ad6605fe7e1d40a336 to your computer and use it in GitHub Desktop.
Save itsdonnix/301b3f554a9f97ad6605fe7e1d40a336 to your computer and use it in GitHub Desktop.
Check PHP syntax
#!/bin/bash
# Usage: check_php_syntax [directory1] [directory2] ... [directoryN]
#
# This script checks the syntax of all PHP files within the provided directories.
# If no directories are specified, it checks the current directory by default.
#
# Example usage:
# check_php_syntax # Check PHP files in the current directory
# check_php_syntax /path/to/dir1 # Check PHP files in a single directory
# check_php_syntax /path/to/dir1 /path/to/dir2 # Check PHP files in multiple directories
#
# The script uses `php -l` to check the syntax of each PHP file and outputs
# whether the file has any syntax errors or not.
# Check if at least one directory is provided, otherwise use the current directory
if [ $# -eq 0 ]; then
echo "No directory paths provided. Using the current directory."
DIRS=("$(pwd)")
else
DIRS=("$@")
fi
PHP_VERSION=$(php --version | head -n 1)
echo "Using PHP $PHP_VERSION"
for DIR in "${DIRS[@]}"; do
if [ -d "$DIR" ]; then
echo "Checking PHP files in: $DIR"
# Loop through all PHP files recursively in the directory
find "$DIR" -type f -name "*.php" | while read -r file; do
# Check the syntax of the PHP file
php -l "$file" >/dev/null 2>&1
# If php -l finds an error, print it
if [ $? -ne 0 ]; then
echo "Syntax error in: $file"
else
echo "No syntax errors in: $file"
fi
done
else
echo "$DIR is not a valid directory!"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment