Jump to content

[SOLVED] Variable is blank, how to substitute?


PHP Nubsauce

Recommended Posts

Hi,

 

I have

 

$username = user_name

$firstname = first_name

$lastname = last_name

addedby = $firstname' . '$lastname

 

How can I make it so if firstname and lastname are blank, it uses username instead?

 

Any help would be appriciated!

 

Thanks,

 

Nubsauce.

 

 

<?php

$username  = $_POST["user_name"];
$firstname   = $_POST["first_name"];
$lastname   = $_POST["last_name"];
$addedby   = "$firstname $lastname";

if($firstname == "" && $lastname == ""){
$username = "$firstname $lastname";
}

// other codez

?>

 

Im not even good in php, but this is just a guess

You should use the empty() function or !isset(). Double quotes sometimes are read as a single space, don't know why, but it has happened to me.

 

<?php

$username  = $_POST['user_name'];
$firstname   = $_POST['first_name'];
$lastname   = $_POST['last_name'];
$addedby   = $firstname . $lastname;

if(empty($firstname)) {
$username = "Username"; //You can change this to whatever you want username to be if it's blank
}
else if(empty($lastname)) {
$username = "Username"; //You can change this to whatever you want username to be if it's blank
}

// other codez

?>

 

You didn't give enough information in your post PHP Nubsauce, so no idea how you want it to be coded.

Ok, I was at work, forgive me, I could not copy and paste my code.

 

$username = $_SESSION['loggedin'];

$firstname = $t['first_name'];

$lastname = $t['last_name'];

$addedby = $firstname. " " .$lastname;

 

This all works fine.

 

All I'd like to do is replace "addedby" with "username" if both firstname and lastname are blank.

 

Thanks for your patience.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.