r/learnphp • • Sep 05 '24

Fatal error: Cannot redeclare StrRev() in /tmp/7tmAJZ6myt.php on line 14

4 Upvotes

Hi,

I have written the following program.

<?php
$arrInt = array(10, 20, 30, 40, 50, 60,70);
$strNumWords= ("First Second Third Fourth Fifth");
arrayRev($arrInt);
StrRev($strNumWords);

unction arrayRev($arrInt){
$arrLen = sizeof($arrInt);
for($i=$arrLen-1; $i>=0; $i--)
echo ("arrInt[$i]=".$arrInt[$i]." ");

}
function StrRev($strNumWords){
echo(" ".strrev($strNumWords));

}
?>

I am getting the error:

Fatal error: Cannot redeclare StrRev() in /tmp/7tmAJZ6myt.php on line 14

Zulfi.


r/learnphp • • May 07 '22

Best place to start

4 Upvotes

Hi all,

I am interested in starting a bootstrapped Saas project, learn as we go. Can you guys recommend a good place to start? I have an idea although it will be a bit to complex at first. Let’s grow in it.


r/learnphp • • Jun 14 '21

How should I check that an image or audio file has been saved to a database?

4 Upvotes

I have code that is supposed to save image and audio files as blobs in a database. I have a PHP script to save example files, but I may need to check that those files have been successfully uploaded. My tables look something like this:

id maybe_some_other_columns... file

and PHPMyAdmin says there are BLOBs in the file columns (which should be the case). Should I check to make sure what's in the file column is the same as what I am trying to save? If so, how should I do that?


r/learnphp • • Jun 09 '21

Print out the content of a text file with a function

4 Upvotes

This should be easy but i'm still new to php and i wanted to turn this piece of code into a function so that i could call it whenever i want and print the content of a text file. This is the code:

$efile = fopen("$file", "r") or die("Couldn't open the file.");

echo fread($efile, filesize("$file"));

fclose($efile);

I wonder if anyone could help me, thanks.


r/learnphp • • Mar 22 '21

DNS config for simple Wordpress app

4 Upvotes

Let's say I create an AWS instance and deploy an app with the following Docker config:

version: "3.9"
    
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}

How do I set up the DNS config on AWS to map the Wordpress page to www.bigmuffin.com? The hostname is 33.203.43.111 and maps to staging.muffin.com. The wordpress page is on 33.203.43.111 :80, right? Sorry, I am a bit confused, because you can set the DNS so that 33.203.43.111 maps to staging.muffin.com and 33.203.43.111:80 maps to www.bigmuffin.com. I didn't think you could do that. Am I wrong? Also, can you use Route53 to do this?


r/learnphp • • Feb 25 '21

So, I have finally finished the course for basic programming, that was mainly written in php language. Where to go next?

4 Upvotes

So hey, I picked up a course like three months ago about basics in programming, and the course was almost entirely written in php language. Now that the course has ended I have no idea where to move next. I would love to code my first blog, but even though the course was fine and highly rated, I don't really feel like I have any idea what to do in coding in general. Could you please point me somewhere? The course really taught me only bacics, for example I have no idea how to make a button that would submit a text, or a comment, and even if I would love to have it on my site, I have no idea where to look for it or how to make it. PHP has so many functions, it is really easy to get overhelmed and lost. Is this something that I experience only as a rookie coder, or it is something that will carry with me forever? Another problem is that whenever I want to create my own code, I am just literally copying things I have learned from the course, I am not really trying to create my own ideas. Are there some good sites where I can learn more, or should I just try to write a code and learn it while doing it? Thanks for every answer!


r/learnphp • • Nov 29 '20

Admin email notification when user registers

4 Upvotes

Looking to learn how to write something that will send the administrator a email when a user registers and needs approval. I am very new to php, I’m essentially stupid to the language. Any help would be much appreciated!


r/learnphp • • Jul 22 '20

What text-editor/IDE do you recommend for PHP?

4 Upvotes

r/learnphp • • Jun 15 '20

Situations where an interface is needed?

4 Upvotes

I was applying for a job as an intermediate developer and the interviewer asked if I were familiar with interfaces, but having written a couple of MVC applications I don't remember having used interfaces like ever. Can someone remind me what are the use cases where interfaces come in handy? I am pretty sure it's mostly useless unless you want to create a sort of base class for database abstraction and then intend on making classes that inherit the interface so they all have the same methods. Aside that, I can't think of a situation where this would be useful and I have no idea why he asked me that.


r/learnphp • • Apr 26 '20

Creating a REST API in php (login/registration system) - CodeIgniter

4 Upvotes

Hello, I just made a video on this, hope it helps anyone:

https://www.youtube.com/watch?v=ilEznG2EqFQ


r/learnphp • • Mar 09 '20

Learning PHP 7 - Good book recommendations

4 Upvotes

Hi Guys,

I am a hobby code who has been playing with PHP for quiet a few years however I am only familiar with PHP 5.6. I would like to learn whats new in PHP 7 and modern best practices - I find working through a book easiest so does anyone have any recommendations.

My concern is most books on amazon do not specify which version of PHP 7 they are teaching and are usually a few years old - I have been warned to avoid learning the early versions of PHP 7 because so many things have changed.


r/learnphp • • Mar 06 '20

Leaning PHP, MySQL & JavaScript, 5th Edition

4 Upvotes

I'm working through Learning PHP, MySQL & JavaScript, 5th Edition and I've realized the problem with books: unlinke online courses, they don't come with a forum to discuss them.

If you are working along with the examples in this book, using the current version of Ampps, you are going to run into some roadblocks, such as

In Chapter 7, Validating uploaded file names, the book says to change an uploaded file name by doing

$name = strtolower(ereg_replace("[^A-Za-z0-9.]", "", $name));

That's not going to work.

Ampps ships with PHP 7.3 The ereg function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

So, my first question is: Is this an appropriate sub to discuss learning PHP with this book?


r/learnphp • • Mar 03 '19

Find something in a source code of a HTML page.

4 Upvotes

I am building a php website which will get some info from another webpage,

I haven't typed any code, because I have no idea what I have to do type, but this idea came to my mind:

  1. PHP will get the source code of the target webpage using file_get_contents()

  2. The target webpage's source code is something like this:

<html>
<head></head>
<body>
<!--some non required elements here-->

<div id="required_info">
info which is dynamic
</div>
</body>
</html>

3. Now I don't know how to get "info which is dynamic" from this source code.

Please help!

NOTE: I don't know anything about RegEX, so I didn't try it.


r/learnphp • • Feb 10 '19

Returning false and a value from function?

4 Upvotes

Hi everybody, I'm making a shopping cart functionality, I have a function Ifor checking if the items in the cart have enough stock left (comparing cart items quantity and product stock left).

With this function I would do something like:

$hasStock = $this->hasEnoughStockLeftToBuy( $cartItems ) ;

if($hasStock){
    //charge customer and empty cart
}

I would also like this function to return the id of the cart item that checks negative(not enough stock left) but I don't know how to do this, I can only think of returning true or false, how can I say: this item has no stock left, also return the id of said item.

This is what I have so far:

public function hasEnoughStockLeftToBuy($cartItems){
        foreach($cartItems as $item){
            $quantity = $item->quantity;
            $discount = Discount::getBySlug($cart->slug);
            $stockLeft = $discount->stock_left;
            if($quantity < $stockLeft){
                return false;
                //any way to return false along with item id?
            }
        }
        return true;
    }

This might be a noob question but any advice or suggestions is greatly appreciated!


r/learnphp • • Jan 10 '18

Hacklines - The Google of all PHP programming articles, videos, and podcasts

Thumbnail hacklines.com
4 Upvotes

r/learnphp • • Sep 11 '17

I'd like to share my open source project for creating php projects quickly

4 Upvotes

Years ago, there was a project called usercake that handled user management in procedural php. I did a complete rewrite and have used it as basically a quick way to write php apps.

  1. It handles all the user management (passwords, resets, login/logout).

  2. Gives simple tools to access the database.

  3. Provides basic bootstrap compatibility.

  4. Offers security features such as CSRF tokens, strongly encrypted passwords, form data sanitization.

  5. An awesome community of people who love to help noobs with a "no question is stupid" mentality.

It's called userspice at userspice com and I hope you find it useful. It can be installed in about 30 seconds by anyone who can create a database.

I'd love some feedback.


r/learnphp • • Apr 02 '17

Looking for a book to deepen my PHP skills

4 Upvotes

I've been using PHP for 7 months now and in I've built some applications using Laravel and YII2 frameworks. The thing is I've been learning PHP on the fly and I think my core PHP knowledge is a bit shallow, tbh. I have the basics of OOP from using a lot of Java in the past, I know namespaces and some other little things, but I think there's still a lot to learn. So, bottom line, I'm looking for a good and a bit more advanced book on PHP. Does anyone know a good book like that, any recomendations? Thanks in advance.


r/learnphp • • Jul 07 '16

PHP Tutorials for Beginners - Great series

Thumbnail
youtube.com
4 Upvotes

r/learnphp • • Dec 30 '15

How to organize code?

4 Upvotes

PHP is my back-end language of choice, but that doesn't mean I'm very good at it. I'm completely self-taught with no formal classes or anything. I'd like to get better at programming (especially Object-Oriented), but baby steps I suppose.

My question now is I'm struggling with knowing how to organize code. For example, I built a small time-clock application (see it on my github here https://github.com/brycematheson/timeclock), but it seems a jumbled mess to me.

What are best practices for organizing code? For example, I have a 'logout.php' file that contains only 4 lines of code that could easily be placed into a function and called whenever the user clicks 'logout' from the top menu.

I'm just confused as to what the best practices are, and where I can go to learn them.

Thanks!


r/learnphp • • Nov 25 '15

PHP web development book for intermediate programmer

4 Upvotes

I would say I am an advanced junior/intermediate web developer, using mostly java on the backend and have some frontend knowledge with html/css/javascript ... I know and used some PHP before but mostly basics (form processing, without OO and frameworks).

As PHP is widely used I believe it is nice to have basic grasp of it even if you are not using it everyday, as some web concepts it implements are pretty straight forward and good for understanding of how web works. I would like to reach level where I can say I know enough to join a project (and not necessarily be very good/expert) and I would like to spend few hours a week learning it (besides doing regular work).

I am looking for a book to teach you intermediate PHP web development and good practices (without teaching you basic programming logic).


r/learnphp • • Jun 01 '15

Best way to include header/footer

4 Upvotes

I have been trying stuff out, but I couldn't find out how to include files the best way. I have seen it in a way that the header/footer were only included once, and not in all the recurring files.

Thanks in advance.


r/learnphp • • Aug 07 '14

Using $_GET

4 Upvotes

Let's say I want to be able to pass a variable to a PHP script by GET sometimes and not others.

Would it be acceptable to use:

if($_GET["time"]){
    echo date(DATE_RSS);
    exit(0);
}

Or would it cause an error when $_GET is not set? What would be the best practice for this situation?


r/learnphp • • Jul 11 '14

"Undefined class Route", having a fantastic start with the Laravel quickstart guide.

4 Upvotes

I'm trying out Laravel installed through composer and I'm following this quickstart guide. I'm currently at the routing section and I've copied the supplied code to apps/routes.php but PHPStorm starts complaining. It says "Undefined class Route" which makes sense since I haven't imported the right class. However there's four to choose from and none of them have a 'get' static method.

What am I missing? Which is the right class to import? These are available to choose:

  • Symfony\Component\Routing\Route

  • Symfony\Component\Routing\Annotation\Route

  • Illuminate\Routing\Route

  • Illuminate\Support\Facades\Route


r/learnphp • • Jul 07 '14

Collaborative project for beginners

4 Upvotes

Hello,

Since I first learned of it (a few months ago) I have wanted to become a part of the open source community. But what can a beginner really contribute? The only technology I know is the basic MAMP stack. Perhaps there are bugs in there...I don't see them.

So I had this idea while laying in the hammock a few minutes ago: why don't I start a collaborative project for beginners. It would be a great way to learn technologies like Git and would give some drive to anyone who is pouring through tutorial after tutorial, exercise after exercise and not feeling the real world application. It really comes down to finding a community of developers who want to learn but haven't got the chops yet to dig through Laravel source code. Also, we would need a project. I vote for some kind of game but I am absolutely open for suggestions.

I have only basic CRUD (reading and writing to MySQL) abilities and the most rudimentary understanding of Object Oriented Programming. The only project I can show for myself is this very ugly blackjack game.

Please reply if you are a total n00b but would like to get better.


r/learnphp • • Jul 07 '14

What are some good online php resources?

4 Upvotes

When I was learning Ruby, I found rubymonk to be quite helpful. Can you guys provide a list of good places on the web to learn php? I know only simple php (mysqli query and stuff like that for simple databases) but would like to know a lot more, especially full web apps and such.