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.

 

 

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

<?php
$username = $_SESSION['loggedin'];
$firstname = $t['first_name'];
$lastname = $t['last_name'];
$addedby = $firstname. " " .$lastname;

if(empty($firstname) && empty($lastname)) {
        $addedby = $_SESSION['loggedin'];
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.