1

Knowing that I can not make a IF statement a variable, how would I make the statement globally accessible within a function? Would I use a ternary operator but if I do how do I make that globally accessible with the Global() function?

project background: This is wordpress so in my functions file I am trying to place the if statement and then access on various other pages. I have a number of other variables in the global scope already so the set up is there between the pages etc, I am just not sure how to globally access the if statement

My If statement I am trying to make accessible from multiple pages is

 if($total_result > 0 && $endResult->posted_by == 'customer'){
                echo '<i class="fas fa-comments-dollar"></i> <br> Client requested a reduced price. <br> They asked for $' . $endResult->price_quote;
            }else if($endResult->posted_by == 'dc_vendor'){
                echo "Waiting for Client response" . '<br>' . '<i class="fas fa-hourglass-half"></i>';
            }else{
                echo "Please check availability and respond to client with availability and price";
            }

UPDATE 1 IDEA:

function vendor_dash_client_response_action($total_result,$end_result) {
    
    global $vendor_dash_client_response_action ;
        if($total_result > 0 && $endResult->posted_by == 'customer'){
                    echo '<i class="fas fa-comments-dollar"></i> <br> Client requested a reduced price. <br> They asked for $' . $endResult->price_quote;
                }else if($endResult->posted_by == 'dc_vendor'){
                    echo "Waiting for Client response" . '<br>' . '<i class="fas fa-hourglass-half"></i>';
                }else{
                    echo "Please check availability and respond to client with availability and price";
                }
};
add_action( 'after_setup_theme', 'vendor_dash_client_response_action' );
Manny
  • 93
  • 6
  • This totally depends on how your project is set up. For example, do you have a single file which loads whatever content you are trying to display? Is every page of your app a completely separate PHP file? Are you using a framework? Sessions? MVC? – GrumpyCrouton Apr 15 '21 at 18:07
  • no frame work, all core php. This is wordpress so in my functions file i am trying to place the if statement and then access on various other pages. I have a number of other variables in the global scope already so the set up is there between the pages etc, I am just not sure how to globally access the if statement – Manny Apr 15 '21 at 18:22
  • Why not just throw your if statement into a function that you pass `$total_result` and `$endResult->posted_by` and return the correct string? Or just assign the correct string to a variable and use that? – GrumpyCrouton Apr 15 '21 at 18:23
  • updated above.,. something like that would work? – Manny Apr 15 '21 at 18:28
  • Is your function file accessible from any page? You almost never want a function to `echo` anything on it's own... Also, I think global variables [are not best practice](https://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice-if-so-why) – GrumpyCrouton Apr 15 '21 at 18:29
  • the functions file can be accessible from any page via the `include_once` . I am opened to other methods that would make it globally accessible. This function gets run from a few pages so it seemed silly to copy/paste the same code into more than one page. It is something that will also be eventually an AJAX action function – Manny Apr 15 '21 at 18:33
  • "_This function gets run from a few pages so it seemed silly to copy/paste the same code into more than one page_" - this is exactly what a function is for, you do all of the code inside a function and make it return the value you need, then you only need to call that function on every page instead of including the entire if/else blocks. – GrumpyCrouton Apr 15 '21 at 18:47

1 Answers1

0

I would create a function, or just assign the string to a variable directly.

function vendor_dash_client_response_action ($total_result, $endResult) {
    switch($endResult->posted_by) {

        //if posted_by = customer
        case "customer":

            //if $total_result is not more than 0, break. This ends the Switch statement, thus running the return statement AFTER the switch
            if($total_result <= 0) break;
            
            //will only run if $total_result > 0
            return "<i class=\"fas fa-comments-dollar\"></i><br>Client requested a reduced price.<br>They asked for $" . $endResult->price_quote;

        //if posted_by = dc_vendor
        case "dc_vendor":
            return "Waiting for Client response<br><i class=\"fas fa-hourglass-half\"></i>";

    }
    
    //if none of the cases above are met, return this
    return "Please check availability and respond to client with availability and price";

}

Now from any page you can call this;

$dash_client_response = vendor_dash_client_response_action($total_result, $endResult);
echo $dash_client_response;

You could also replace this function with your existing if/else structure and just use return instead of echo.

GrumpyCrouton
  • 7,816
  • 5
  • 27
  • 61
  • ah that makes sense. Is there a way on the switch statement to include both conditions from the if statement? – Manny Apr 15 '21 at 19:12
  • @Manny The code I posted already has both conditions. Will update my post to show how it works. Edit: Oh, I see what you mean, one sec – GrumpyCrouton Apr 15 '21 at 19:15
  • @Manny Check my update please – GrumpyCrouton Apr 15 '21 at 19:22
  • That is great thank you so much for showing me that and taking the time to update the above. That makes a lot of sense and I have been reading about why not to use echo in a function so the switch makes all the sense – Manny Apr 15 '21 at 19:28
  • @Manny The Switch isn't really a requirement, I just thought that it is easier to read than if/else statements. You could easily use your existing if/else blocks and just replace the word `echo` with `return` and it would work just the same. Happy coding! – GrumpyCrouton Apr 15 '21 at 19:31
  • thanks the switch is cleaner :) – Manny Apr 15 '21 at 19:35