PHP Echo Variable/String in HTML + Good book for PHP Programming?


I do apologize as my post is going to sound as idiotic as it probably gets on here, but I can’t seem to find a clear resolution to the problem (please note that I know nothing of PHP yet am looking to learn).

So I recently started my own little project that uses Steam as an OpenID provider to log in. This presents the problem, OpenID. I know nothing of it and was able to get as far as loading a library onto the site and getting the login function setup. Now I am presented with a problem: I don’t know how to implement the retrieved user ID.

Echo/Print Variable in HTML

According to the documentation on their site, Steam’s OpenID provider only provides the user’s SteamID64. I am able to make do with this for the project, but I would first need to know how to implement the ID as a variable into HTML. I would have to use two other PHP files, one to match the ID to a username and another to use the ID for it’s general purpose within the project, but that is to come later. I just need it to display PHP variables in an element.

I believe there is an echo function that I would probably have to use, but I am not all too sure as to how I’d implement it into an HTML component (it’s probably really simple, but again, I know nothing of PHP).

PHP Book?

I really don’t want to keep asking people for help when I start projects like these. So that’s why I just want to generally find a book that will give me the basics on how PHP functions and what I need to know to start using it. I already have the knowledge of Java, HTML, CSS and command-line languages (I know, none of these are impressive, laugh all you want). I just need to know the basics of PHP so I can get stuff done. Does anyone know of a book which I can buy (preferably through Google Play) to help me accomplish this?

Thank you for taking the time to read all this and I do apologize for the very stupid questions that I am presenting. I also apologize for lengthening my post by talking so formally, but my past experiences of stating my problem simply and being degraded for it leaves me in the mind-set that a full explanation and trying to make my post sound at least somewhat intelligible is of necessity.

Thanks again

Welcome to Sitepoint Kdog.

Okay, so you’re new to PHP. The first thing to understand is that, as far as PHP is concerned, HTML is just meaningless text.

If you have any variable storing a string (generally text) or a number, you can simply output it with the ‘echo’ command as you suggested. If you’re in the middle of HTML, it doesn’t matter, as long as the file is parsed by PHP. A simplistic example:

$userID = 0;
// connect to steam, get results, set $userID to the user's ID.
    <!-- ... -->
            <h1>User Information Page</h1>
        <p>Your user ID is: <?php echo $userID; ?></p>
        // if you'd like to use the variable in JS, don't worry, PHP doesn't know or care what's around it.
        var userID = <?php echo $userID; ?>;
        // from here you can use the userID variable in javascript, as when it hits the user it will have the number printed there instead.
        alert("Hi, I'm an annoying alert. But I bring a message from the server-side: your user ID! It is " + userID);

With regards to books, I would like to suggest that you first find and follow some online tutorials. Come up with a certain type of problem (shopping cart, etc), and search for it - just by doing that, you’ll learn a lot about databases, using sessions, eventually some object-oriented PHP. That’s exactly the route I took many years ago.

Until then, don’t be afraid to ask questions here. You’ll get some especially good responses if you ask more theoretical questions, such as “what are the different ways of approaching x” or “This is what I know, this is the kind of code I currently write. What should I learn next, and how can I improve on what I already know?”. It might take a little longer to go about it in this way, but trust me - many PHP book authors know less about PHP than some of the people on these forums.