Zdravko Nikolov
Zdravko Nikolov

Reputation: 35

php get for POST or use value

I have a php function which gets a value from previous page but I want to have an existing value just in case if I leave the field empty.

Example:

$age = $_POST['age']; 

want to add if statament if age is empty user 50 as a value

Upvotes: 1

Views: 58

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

<?php
//Standard If Else 
if(isset($_GET['age']) && is_numeric($_GET['age'])){
    $age = $_GET['age'];
}else{
    $age = 50;
}

// Or a One-line Ternary
$age = isset($_GET['age']) && is_numeric($_GET['age']) ? $_GET['age'] : 50;
?>

Upvotes: 0

Green Black
Green Black

Reputation: 5084

$age = isset( $_GET['age'] ) ? (int) $_GET['age'] : 50 ;

Upvotes: 1

Related Questions