PHP password_algos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Password

Definition

In PHP, password_algos() returns a list of the password hashing algorithms your server supports.

Use it to check that an algorithm such as Argon2 is available before you choose it.

Syntax

Call password_algos() with the values you need. Start with this line.

PHP
$algos = password_algos();
CallWhat it does
password_algos()Returns an array of algorithm IDs.
in_array('2y', password_algos(), true)Checks that bcrypt is available.

bcrypt (ID 2y) is always there. Argon2 IDs show up only if PHP was built with Argon2. Added in PHP 7.4.

Library declarations
PHP
password_algos(): array

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
arrayA list of algorithm IDs such as "2y", "argon2i" and "argon2id". The list depends on how PHP was built.

PHP Examples

Example 1 — Check That bcrypt Is Available

Look for the bcrypt ID in the list.

PHP
<?php
$algos = password_algos();

if (in_array('2y', $algos, true)) {
    echo "bcrypt is available", PHP_EOL;
}

How It Works

  1. Call the function. password_algos() returns an array of strings.
  2. Search the array. in_array() looks for 2y, the bcrypt ID.
  3. Use strict mode. The third argument true compares types too.
  4. Print a message. bcrypt is always built in, so this always runs.

Example 2 — Constants Are Just IDs

The password constants hold the same IDs as the list.

PHP
<?php
echo PASSWORD_BCRYPT, PHP_EOL;
echo PASSWORD_DEFAULT, PHP_EOL;

if (PASSWORD_BCRYPT === '2y') {
    echo "Same as the list ID", PHP_EOL;
}

How It Works

  1. Print PASSWORD_BCRYPT. Since PHP 7.4 it is the string 2y.
  2. Print PASSWORD_DEFAULT. Today it also points to bcrypt.
  3. Compare with the ID. The values match the IDs from the list.
  4. Remember the rule. Use the constants in code and the list only to check support.

Example 3 — Pick the Best Available Algorithm

Prefer Argon2id when it is supported, otherwise use bcrypt. The demo uses two fixed lists so the output never changes.

PHP
<?php
function pickAlgo(array $available): string
{
    return in_array('argon2id', $available, true) ? 'argon2id' : '2y';
}

echo pickAlgo(['2y']), PHP_EOL;
echo pickAlgo(['2y', 'argon2i', 'argon2id']), PHP_EOL;

// On a real server: pickAlgo(password_algos())

How It Works

  1. Accept a list. The function takes any array of IDs.
  2. Look for argon2id. If it is there, choose it.
  3. Fall back to bcrypt. 2y works on every PHP 7.4+ server.
  4. Pass the real list. On a live server call pickAlgo(password_algos()).

Frequently Asked Questions

It returns an array of the password hashing algorithm IDs your PHP build supports, such as 2y for bcrypt and argon2id for Argon2id.
password_algos(): array. It takes no parameters.
PHP 7.4. On older versions the function does not exist, so check function_exists('password_algos') first.
Argon2 is optional. PHP must be compiled with Argon2 support (libargon2 or libsodium). If it is missing, use bcrypt through PASSWORD_DEFAULT.
No. Pick one algorithm, usually PASSWORD_DEFAULT, and call password_hash(). Use password_algos() only to check support.

Next: password_get_info()

password_get_info() reads the algorithm and cost from a hash.

password_get_info() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful